[swift-evolution] Operator implementation inside struct/class body

Jessy Catterwaul mr.jessy at gmail.com
Mon Feb 8 16:02:28 CST 2016


> After reading your messages, however, I have to agree that the second approach, using instances members, is a much better one. 

Agreed. I think C# only uses static functions because it doesn’t have module-scope operators like Swift does. 

Here’s what instance operators would look like with shorthand argument names:

struct Vector2 {
   func * (Self) -> Self {
      return Self(x * $0.x, y * $0.y)
   }
}

While I believe that instance operators would represent the vast majority of operations, operators that use operands of different types don’t make sense to be defined in a type. I love that Swift allows that sort of thing at module scope.

func * (matrix: Matrix2x2, vector: Vector2) -> Vector2 {…

I bet it makes sense to enforce that an operator is only allowed at module scope if its operands aren’t of the same type.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160208/1c9e0072/attachment.html>


More information about the swift-evolution mailing list