swift - Why is `infix` the default -
i'm reading advanced operators in swift.
a basic function:
func add(a: unicorn, b: unicorn) -> unicorn { return + b } add(u1, u2) // 2 unicorns added an infix operator:
func + (a: unicorn, b: unicorn) -> unicorn { return + b } c = u1 + u2 postfix:
postfix func + (a: unicorn) -> unicorn { return + 2 } // nonsense example u1+ i don't understand how infix can presumed default. doesn't different me normal function declaration.
what rule here--is single character legal name infix operation? mean single char functions not otherwise allowed? can any two-argument function in swift called infix style?
i don't understand how infix can presumed default.
if implement operator 2 arguments compiler can sure want infix operator, because infix operators operate on 2 values.
if implement operator 1 value compiler isn't sure whether either prefix or postfix operator , complain.
infix operators take 2 values, postfix , prefix one.
does mean single char functions not otherwise allowed? can two-argument function in swift called infix style?
no. function begins of these characters operator: /, =, -, +, !, *, %, <, >, &, |, ^, ?, or ~1. operator can consist of multiple characters, must begin 1 of these characters.
remember new operator must declared first, instance this:
prefix operator / {} prefix func / (a: int) -> int { return / 42 } /56 however declaration won't work, because a not 1 of characters operator must start:
prefix operator {} 1 there more characters. read here.
Comments
Post a Comment