UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Swift Standard Library - How are operators implemented

Forums > Swift

Hi all,

I recently watched Paul's excellent https://youtu.be/wB-bi8_rSLs?si=bGmNey5r1zies3Oh video on the Swift Standard Library. I've gone done a bit of a rabbit hole and I started wondering how the basic operators such as + - / * are implemented. I can't find a file in the source code which defines them, other than some code like the code below, however, this seems to define + and simultaneously uses it in the function body. My guess is that the operators are 'built in' to the compiler itself, or perhaps defined elsewhere in a C file or such like. Curiosity has got the better of me so I apologise if this is a stupid question.

// Inside the UInt16 implementation
public static func +(_ lhs: UInt16, _ rhs: UInt16) -> UInt16 {
  var lhs = lhs
  lhs += rhs
  return lhs
}

1      

The fine folks over at the Swift language forums should be able to answer this for you. Quite a few of the compiler team post over there regularly and the discussions usually lean towards the more technical side of the language than this site, which tends to be more oriented towards iOS and macOS development.

2      

For anyone interested, the kind people over at Swift Forums did indeed get me out of the rabbit hole. Thanks @roosterboy.

https://forums.swift.org/t/implementation-of-standard-operators/70079

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.