I've seen this done, but I just can't wrap my head around it. Somehow, seemingly magically, some infix functions work fine, but others simply won't compile. For example:
As you see here, my then
function work as a traditional function, but not as an infix one, yet my *
one has the opposite issue. What's the magic sauce to get my then
function to be an infix one?
Side question: Why won't my *
function work as a traditional function?
Code for text-only readers and copy-pasting:
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 ';'