[ANN] Operator overloading, the big hack

314 views
Skip to first unread message

Jesse van den Kieboom

unread,
Sep 23, 2013, 12:51:46 PM9/23/13
to golan...@googlegroups.com
I would like to announce a little project that I started to allow operator overloading in Go. go-operators is a tool which transforms Go sources such that operators on types are translated to corresponding method calls on those types if available. It basically converts a Go source to an AST using go/ast. Then, a patched version of go/types walks over the AST, and resolves operators (binary and unary) on normally unsupported types to method calls on those types, if possible.

I don't want to start a discussion on whether or not operator overloading is a good idea. Personally, I'm happy that Go doesn't support operator overloading in the language in general, except for the case of writing some numerics code (which arguably you shouldn't with Go). I'm fully aware that this is a hack, but it's also a hack that works.

The source of go-operators is available at https://github.com/jessevdk/go-operators/

For an example of operator overloading, see http://play.golang.org/p/qw5enJa9lY

Nate Finch

unread,
Sep 23, 2013, 3:25:27 PM9/23/13
to golan...@googlegroups.com
Neat. I'd love to see a way to integrate this with gocog so that the replacements could be done inline in the same file.

It would take a little tweaking to your code (and possibly gocog), but then you could do something like this:

Start with this code:
/* [[[gocog 
ret := 2*v1*v2 + v1 - 4 
gocog]]] */
// [[[end]]]

after generation, have this code:
/* [[[gocog 
ret := 2*v1*v2 + v1 - 4 
gocog]]] */
ret := v1.Op_PreMultiplyScalar(2).Op_Multiply(v2).Op_Add(v1).Op_SubtractScalar(4)
// [[[end]]]


The nice thing about that is the generator text and the output text stay next to one another.  Obviously, it's not as clean as it is in your version... but it could be cleaned up (with some tweaks to gocog's current code) to look something like this:

/// ret := 2*v1*v2 + v1 - 4 
ret := v1.Op_PreMultiplyScalar(2).Op_Multiply(v2).Op_Add(v1).Op_SubtractScalar(4)


...in fact I may go back and see if I can implement this style sometime very soon.

John Nagle

unread,
Sep 23, 2013, 9:14:24 PM9/23/13
to golan...@googlegroups.com
On 9/23/2013 9:51 AM, Jesse van den Kieboom wrote:
> I would like to announce a little project that I started to allow operator
> overloading in Go. go-operators is a tool which transforms Go sources such
> that operators on types are translated to corresponding method calls on
> those types if available. It basically converts a Go source to an AST using
> go/ast. Then, a patched version of go/types walks over the AST, and
> resolves operators (binary and unary) on normally unsupported types to
> method calls on those types, if possible.
>
> I don't want to start a discussion on whether or not operator overloading
> is a good idea. Personally, I'm happy that Go doesn't support operator
> overloading in the language in general, except for the case of writing some
> numerics code (which arguably you shouldn't with Go).

Go is potentially a good, fast language for numerics, and I've been
pushing for multidimensional arrays. But others have convinced
me that overloading operators in Go is a bad idea, even for math
operations.

I could become convinced that overloading + and - is a good
idea. But even * gets complex. Dot or cross product? What gets
called for vector * matrix? That's why NumPy uses X.dot(Y)
and X.cross(Y), but overloads + and -.

Overloading binary operators in a language that doesn't
support multiple argument overloading means you have to define
your own overloading semantics.

John Nagle

Reply all
Reply to author
Forward
0 new messages