Ternary magical? I think not

Swift standard library operators are defined in Swift itself, using the same facilities that allow you, the user, to define your own custom operators. Implementations of standard library operator functions use the same language features that you would use for your own functions. You can read the source code for &&, for example, and understand what's going on based on your knowledge of Swift alone.

It is not possible to declare the ternary operator in Swift: there is simply no syntax to do so, and for this reason you cannot create any of your own ternary operators. You cannot read the Swift source code implementing the ternary operator ?:, because there is no such source code; you cannot even find the Swift declaration of the operator, because there is no such declaration--it cannot even be uttered.

How can a function exist without a declaration or implementation? In the parlance of the Swift project, we call this "magic." It is simply a term used to describe APIs or behaviors supported by the language where the underlying implementation cannot be accounted for purely in terms of the user-facing rules and features of Swift.

8 Likes