First off, I think that's the most reasonable proposal for operator
overloading that I've seen in go, since it doesn't conflict with things
like sort.Interface's Less method, and the overloaded cases would "stand
out", thus somewhat mitigating the "hidden costs" counter-argument. That
said, I probably would avoid using them on principle, even if added to the
language, and I do see some potential problems that would need to be
accounted for or explained away before the proposal would be consistent
enough to be a candidate feature:
How would order of operations be addressed? Somebody somewhere is going to
complain that, in their particular case, + should have a higher precedence
than *, and since anyone would be able to dispense with sanity anyway (by
having side effects and un-math-like semantics), it'd be hard to argue that
their desire is wrong.
Also, some would desire for any number of consecutive, equivalent,
applications of an operator, to result in a single invocation.
For example, `func(T, ...T) T` instead of `func(T, T) T` -- i.e. `t1 + t2 +
t3` being sugar for t1.`+`(t2, t3)
(an aside: I agree with Ian about the backticks if only because it makes
this syntax difficult to explain conventionally).
This would be important for efficiency if anyone wanted to overload
operators for set logic (when it is generally more efficient to iterate
over smaller sets when performing an intersection). Conceivably people
would also want this capability anyway, "just because", since the door
already would've been opened to potentially insensible behavior.
Also, how would you handle unary operators? `^` as a unary vs `^` as a
binary operator, as well as `+`, `-`, and `<-` ? `&` (address of) and `*`
(deference) would be a bit absurd, but there wouldn't be any consistent
justification in preventing use of those.
If `!` were allowed as a unary method, or `==` as a binary method, would
they be required to return a bool, or could they return T? If they returned
a bool, it would be argued that the mechanism should evolve beyond
syntactic sugar, and that anything implementing `!` be allowed in any
boolean context. For example:
type Boolish struct {}
func (b Boolish) `!`() bool { return true }
if Boolish{} { /* do something */ }
If that were allowed, then it would certainly mean that at least one
predefined interface (the "Banger" interface?) would end up in the language
spec itself, and it'd then be a bit hypocritical to keep out standard
iterator interfaces (special cased by range loops), and to some extent,
making the functionality in strings proper methods on the builtin string,
etc.
On Thursday, February 7, 2013 2:58:19 PM UTC-7, Scott Pakin wrote:On Thursday, February 7, 2013 1:39:24 PM UTC-7, Tim Harig wrote:
It isn't about saving characters, it is about making the formula look
more
like its mathematical notation. That makes it much more intuitive and
easy
to follow for those who are used to seeing equations on paper.
Hey, here's a crazy compromise idea: Go could allow operators to be used
as method names but require that they be decorated in some fashion to
distinguish them from built-in operators, for example surrounding them with
backquotes (à la Haskell's "treat as infix" syntax). Thus, a `+` b `*` ccould be syntactic sugar for
a.`+`(b.`*`(c)). This should give the best of both worlds: a visual
distinction between built-in and user-defined operators combined with the
ability to write more readable, math-like expressions.
Oh, and for crazy compromise idea #2, backquotes (or whatever) could more
generally be used, Haskell-like, to invoke a method of type func(T, T) Tusing infix notation. Hence, a
`Myfunc` b would be syntactic sugar for a.Myfunc(b). The differences
with crazy compromise idea #1 are that (1) it adds only a calling
convention without changing how identifiers are lexed, and (2) all such
invocations have equal precedence so one would have to parenthesize to
control the order of operations: a `MyAdd` (b `MyMult` c). Note that
these two ideas are orthogonal to each other.
What do you guys think? Have I finally found a solution to the
operator-overloading versus no-operator-overloading debate that makes
everyone happy?
— Scott
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/groups/opt_out.