[Grammer] 26 - Custom Operators

gunw(25)
Published in
#swift4
Words
11
Reading
1 min
Listen
Play
8y
  • Custom Operators : prefix, postfix, infix 중 하나를 커스텀
infix operator *+* : MultiplicationPrecedence // 우선순위 그룹 설정
extension Int {
   static func *+*(left: Int, right: Int) -> Int {
      return (left * right) + (left * right)
   }
}
1 *+* 2 + 3 // 7
  • precedencegroup custom
precedencegroup MyPrecedence {
   higherThan: AdditionPrecedence
}
infix operator *+* : MyPrecedence // 우선순위 그룹 설정
extension Int {
   static func *+*(left: Int, right: Int) -> Int {
      return (left * right) + (left * right)
   }
}

1 *+* 2 + 3 // 7
[Grammer] 26 - Custom Operators | Ecency