-
Notifications
You must be signed in to change notification settings - Fork 156
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
Smooth Operator Overloading #284
Comments
(Ref satyr/coco#60)
|
thx, fixed example |
I suppose we could use |
neat idea! |
Maybe it's possible to just have some utility properties, that when defined, overload the operator without any need for more syntactic sugar(think python):
Although, that would be a bit hard to implement at compile-time. |
@gratimax, that approach actually adds an unnecessary renaming, hides the fact that overloading is happening, and requires runtime support. |
@brainrape I guess you're right, then. +1 |
Trying to gauge the temperature in the room: I would love a way to overload array indexing and attribute access... actually way more than even arithmetic operators. Not sure if mapping to something like python's getattr and getitem methods are the way to go, but maybe something similar? |
objects / length star / defineProperty :° ? |
Not sure what you mean by that? Are you saying LiveScript already provides some ability to programmatically control attribute access? because as far as I can tell Javascript itself doesn't, and one would have to fake it. It would definitely be a lot slower than built in object attribute or array item access. |
using |
yeah, that's not the same thing. I mean programmatic access for intercepting attribute and indexing accesses as keys. A heavily contrived Python example: class Dog(object):
def __init__(self,name):
self.name = name
self.tricks = {}
def teach(self,name,trick):
self.tricks[name] = trick
def __getattr__(self,name):
if name in self.tricks:
return self.tricks[name].__get__(self)
return lambda : self.name + " tilts head"
dog = Dog()
dog.teach("sit", lambda x: x.name + " sits")
dog.teach("speak", lambda x: x.name + ": Woof")
print dog.speak() # "fido: Woof"
print dog.sit() # "fido sits"
print dog.lay() # "fido tilts head" |
Ah. Proxies then. |
yeah, that looks about right... just wondering about how people feel about this in livescript since it can be thought of as a form of operator overloading. Though, come to think about it... can't see how implementing this as the Livescript level wouldn't slow all object accesses down, so.... |
I don't see how we could do that in LS, tbh |
Yeah, can't see how it wouldn't be horribly slow without some kind of runtime support. Hmm, looks like a proxy object is slated for Harmony though... |
i wanted to keep it lightweight, that's why i didn't explore property access and indexing.
but that might conflict with livescript semiautovivification there's also https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/get (and set) |
A long-time missing *script feature is operator overloading.
We can get close, e.g.
but even disregarding the ugly output (and 'class' and 'new'), it's far from perfect:
I propose adding the syntax:
I'm not so sure about these, especially the last one:
These are currently syntax errors, so it shouldn't conflict with anything. This syntax would clearly differentiate overloaded operators while still looking similar to the plain variety. The overloaded operators should have the same arity, precedence and bracket rules as the real ones.
The text was updated successfully, but these errors were encountered: