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

Haravikk swift-evolution at haravikk.me
Tue Feb 9 05:24:04 CST 2016


> On 8 Feb 2016, at 22:02, Jessy Catterwaul via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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.

Would it even need to be that complex? I would say to just allow global scope operators as well, and leave it to the developer to decide if that’s the best place to put it versus putting it into a type.

That said, I’m finding it hard to think of examples that really need to be in the global scope, as your example seems to me like it still makes the most sense to be placed within the Matrix2x2 type like so:

	func * (vector:Vector2) -> Vector2 { // self is matrix2x2 }

Since the left hand side is still the focal point of this operator. The only case I could think of that might still require a global operator would be one that can handle arguments of type Any.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160209/6610f4ad/attachment.html>


More information about the swift-evolution mailing list