Open brainrake opened 11 years ago
(Ref satyr/coco#60)
a\+ b and c
compiles to
a['+'](b) && c;
, not your code : and
closes implicit calls.
thx, fixed example
I suppose we could use doLiteral
in lexer to change tag to LITERAL if last is dot. not sure if that's worth it, considering you have to implem these yourself anyway
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):
myObj =
val: 5
__plus: (that) -> that.val + @val
myObj + {val: 5}
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.
obj.+
syntax overcomes these problems, and you can still use obj'+'
or obj[\+]
etc to perform a regular property lookup.
@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?
I would love a way to overload array indexing and attribute access
objects / length star / defineProperty :° ?
@Nami-Doc
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.
class Object
abc:~ -> "abc"
console.log (new Object)abc
# => "abc"
using Object.defineProperty
@Nami-Doc
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.
@Nami-Doc
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. although indexing could work with
obj.[3] #=> obj['[]'](3)
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.