Reputation: 265
Even though the swift documentation says that we can overload the . symbol I can't seem to do so without the compiler complaining.
Here is what I'm doing:
operator infix . {}
@infix func . (left:Vector2D, right:Vector2D) -> Vector2D {
//func
}
Has anyone successfully done this in swift?
Upvotes: 4
Views: 1412
Reputation: 5559
I think you might misunderstand the documentation.
It is not possible to overload the default assignment operator (=). Only the compound assignment operators can be overloaded. Similarly, the ternary conditional operator (a ? b : c) cannot be overloaded.
.
is obviously not a compound assignment operator, it's a member selection operator.
However, you can
declare and implement your own custom operators in addition to the standard operators provided by Swift. Custom operators can be defined only with the characters / = - + * % < > ! & | ^ . ~.
Upvotes: 1
Reputation: 126187
You can use the .
character in a custom operator, but you can't create an operator that's the .
character alone — it looks like that token is a more basic part of the grammar. (Actually, it looks like there are more restrictions on .
than you see in the grammar part of the language guide, because you can create the extended-silence operator ......
, but not the happy operator ^.^
.)
(Think about it this way: if you tried to use .
to define the dot product of two vectors, you'd have a hard time using it to address members of those vectors.)
Upvotes: 4