How to write Swift infix functions?

I saw this, but I just can't get my head around it. Somehow, it would seem, magically, some infix functions work fine, but others just don't compile. For instance:

Function named

As you can see here, my then function works like a traditional function, but not as infix, but my * has the opposite problem. What is a magic sauce so my then function is infix?

Side question: why does my function * not work as a traditional function?


Code for text readers and copying:

 public func * (let left:String, let right:Int) -> String { if right <= 0 { return "" } var result = left for _ in 1..<right { result += left } return result } *("Left", 6) // error: '*' is not a prefix unary operator "Left" * 6 // "LeftLeftLeftLeftLeftLeft" public func then (let left:String, let _ right:String) -> String { return left + right } then("Left", "Right") // "LeftRight" "Left" then "Right" // 2 errors: Consecutive statements on a line must be separated by ';' 
+6
source share
2 answers

The standard Swift library already defines * as an infix operator:

 infix operator * { associativity left precedence 150 } 

You will find a list of predefined operators in the Operator Directory of the quick reference library . Alternatively, click command in an import Swift statement in Xcode and search for an β€œoperator”.

To use a statement in a "traditional" function call, you must enclose it in parentheses:

 (*)("Left", 6) 

If you want to define your own infix operator, you need to add the infix operator . Please note, however, that only a limited set of characters are valid for operators (see Language Reference-> Lexical Structure-> Operators for exact rules). In particular, the name operator must (as @dfri already said) begin with /, =, -, +,!, *,%, <,>, &, |, ^,?, ~ Or some other characters of the symbol. In particular, then is not a valid operator name.

+6
source

* already defined, initially, as a binary infix operator in Swift:

 infix operator * { associativity left precedence 150 } 

Therefore, any function of the form func * (... will overload this binary infix operator. On the other hand, if you try to use the * operator as a prefix of a unary operator, you will get a descriptive error that " * not a prefix of a unary operator", just since * does not exist initially as a prefix operator.

Of course, you can define your own prefix operator * :

 prefix operator * { } prefix func * (rhs: Int) -> Int { return rhs*rhs } var a : Int = 2 var b : Int = *a // 4 

To wrap this up: some operators exist initially in Swift both as prefix and infix operators, for example. -

 /* Declared natively in Swift */ infix operator - { associativity left precedence 140 } prefix operator - { } /* Example usage */ let a : Int = 2 - 1 // 1, '-' infix operator used let b : Int = -1 // -1, '-' prefix operator used 

while others, such as * , are simply (originally) used as an infix operator.

Also note that if you want to define your own custom infix operators, their resolved names are limited as follows:

User statements may begin with one of the ASCII characters /, =, -, +,!, *,%, <,>, &, |, ^ ,? or ~ or one of the Unicode characters defined in the grammar below (which include characters from Math operators, various characters and Unicode block dingbats, among others). After the first character combining Unicode characters are also allowed.

From Language Reference - Lexical Structure .

+2
source

All Articles