-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: operator overloading #60
Comments
I'm all for this if we can find a simple, clever implementation. Some consideration off the top of my head:
|
Ok, how about replacing all uses of an overloaded operator within the scope that the operator was overloaded. I don't know if this is possible without executing the code during compilation, but how about only changing it on values that have the overloading method. |
Oh, and I don't think a binary function would do it, it would prevent specifying only certain objects to have redefined operators. |
How? Sample code and compilation would be great. |
compiles to:
Again, I have no idea if this is possible, but the function I linked to in the beginning would let you do:
and the operate function would decide whether to use the real operator or the method. Precedence could be resolved by parsing operators with the highest precedence first, and those with lowest last. |
Not easy. We don't have a way to infer
Why not simply |
|
For what? Your compilation figures out edit: Uh I see now. Like I said in the first response, It shouldn't interfere with normal code. It can't slow down all other operations. |
You mean, using JS operators'? What about custom operators? |
I wasn't thinking about custom operators, I don't see what advantage they have over functions, especially as function sin coco don't require parens. You could look into languages like haskell to see how they handle infix function precedence, but I believe that they are just evaluated left to right:
is interpreted as
how about overloading of native operators as an option that's off by default. |
and you are very right to worry about performance of this, I tested
versus
the second took 11 seconds according to the unix I think that having options to enable and disable it for the current scope (with disabled by default) would be useful. I know it might not sound great to have compiler options, but ES5 has them, so there's a precedent. |
Haskell allows you to pick a precedence.
Though inelegant, simply replacing every occurrence of operators under that declaration would be easy. |
I don't know haskell's syntax, so I can't really understand that document. An elegant way to set precedence would be:
That might be somewhat hard to parse because you would have to modify operator precedence on the fly. |
Here's a proof of concept: https://gist.github.com/982183
|
That looks great, though I assume that there would be some syntactic sugar for As you said, It's not quite as elegant of an operator overloading implementation as ruby's, but it's as good a Haskell's, and better than perl 6's infix functions. Thanks for considering my suggestion :) |
Yup, it's just a quick example. You can tweak away the gist for your need (and reach to a more elegant solution along the way ;). |
Ok, I'll see if I can understand the parsing going on. |
Funny, I thought of a similar idea a few days ago. I was thinking of it in terms of Scala's similar feature, where I think the best approach to this problem would be to disallow overloading of built-in operators altogether, and simply have a blacklist of pre-, post- and infix operators (i.e. the built-in ones). I think it also clarifies that "these operators are built-ins" when skimming through code. Regarding operator precedence, I wouldn't bother at all. I'd simply see it as syntactic sugar for regular function invocation. IMHO I'd like the following compilation: |
That's actually pretty close to what we already have:
|
Oh, that's quite nice actually. I guess I'm fine with that syntax. :) |
The problem with the scala syntax is that |
+1 |
Operator overloading is a great feature of languages like ruby and c++, especially for things like matrix arithmetic. I wrote a function for operator overloading (ironically in coffeescript) that works a lot like ruby's operator overloading where operators are methods.
The code is here.
I don't know much about parsing, and as coffeescript is strongly against special functions, I figured I'd ask here. It shouldn't be too hard to implement if you know what you're doing.
The text was updated successfully, but these errors were encountered: