dave0706
(David)
1
After learning how some basic types (Bool, Int etc) are implemented in Swift, I ended up down a bit of a rabbit hole and I started wondering how the basic operators such as + - / * themselves are implemented in Swift. I can't find a file in the source code anywhere in the Standard Library, despite looking for hours. My guess is that the operators are 'built in' to the language/compiler itself, somewhere in the parser files perhaps, maybe even lower. I doubt very much they would be implemented in Swift itself. But I can’t see them at all. Curiosity has got the better of me so I apologise if this is a stupid question.
3 Likes
xwu
(Xiaodi Wu)
2
They are implemented by calling built-in functions in turn, but they themselves are actual Swift functions.
It's just hard to find in the repository because the source code lives in a gyb file ("generate your boilerplate"), which is a templating system that allows repetitive source code which is nearly identical for Int8, Int16, etc. to be written once. This also means that you won't find static func +, static func -, etc., written out verbatim separately, since it's all generated from a single template static func ${x.operator}.
The operators +, -, etc. are implemented here in terms of the corresponding assignment operators, which are implemented here.
7 Likes
dave0706
(David)
3
Amazing. Thank you so much for your detailed reply. No wonder I couldn’t find it. Very surprised to see they are Swift functions. Thanks once again.
1 Like
AlexanderM
(Alexander Momchilov)
4
Well they're not exactly. Those functions are just wrappers for the underlying built-ins, like Builtin.int_sadd_with_overflow_Int16.
2 Likes
dave0706
(David)
5
Thanks Alexander. I won’t lie this is going to take me a while to work through, thanks for the pointers of where to start.