-
Notifications
You must be signed in to change notification settings - Fork 15
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
Implement object literals #25
Comments
Oh, and happily, since we've now gotten rid of code blocks as a syntactic thing, there would be no parsing ambiguity between code blocks and objects. Although since we still have |
To avoid users having to linearly scan an object to check whether an object has a given property, we should also provide a Making a lookup on a property that does not exist on the object should be a lookup error, just like out-of-range array lookups, and just as it is in Python:
I briefly considered making an exception for
instead of
But let's not do that. Weirdly, I think it would be helpful to have that shortcut, but it doesn't feel elegant, and it introduces a bit of magic that involves both the runtime (which would usually throw that property lookup error) and the parser (which would have to be involved in silencing the error, since the exception is syntactical in nature). |
Actually — it strikes me now — it would make a lot more sense to have These methods would be provided as if inherited/linked from a general The question presents itself what would happen if the object literal a contained |
Though maybe I also got to thinking whether we should allow setting properties that don't exist on the original objects. (I.e. should we allow non-overriding property writes?) On the face of it, that seems perfectly harmless, but I think conservatively forbidding it might make life easier for us once we get to typing and #33. Specifically, something like this:
would implicitly type the variable So maybe we should simply disallow it from the start. Post-rationalizing, maybe it's good for the soul to predeclare all the properties you plan to change even in the original object literal. |
The answer, in retrospect, is obvious: eschew the von Neumann bottleneck, avoid anemic domain models! Naturally, the obvious way to update properties is to use object literals! I propose
|
|
As I'm about to implement this, I'm now tempted towards (b) instead. 007 (just as Perl or Python) isn't particularly keen to forbid things that you might as well override for some reason:
Though sometimes the built-in thing wins because it's special-cased in the parser:
In the case of user-overridden properties, though, I think it makes the most sense for the user's version to win. Rhymes well with the tendency for late-binding in objects, too. Also, it does seem we'll get some reluctant kind of inheritence, so it's easy to motivate from that angle, too. |
All the follow-up actions are now done. Closing. vendethiel++ for original patch, and giving us objects just when it became interesting to have them for synthetic Qtrees. :) |
Very belatedly, I now think that Interestingly, there's a recent ES proposal which seems to make the same point, belatedly proposing I take this general sentiment ("put reflective/meta stuff outside of the object-level stuff") to be the driving force behind Bracha/Ungar's ideas about mirrors (ACM link) — without claiming to understand that work in detail. Indeed, from the abstract of that paper: "Stratification: meta-level facilities must be separated from base-level functionality". |
Objects, if we get then in 007, will be immutable, just like arrays. (Implementing mutation isn't worth it for us, and having everything be immutable has some nice benefits, too.)
Here's what I'm proposing for syntax, based on ES6:
The run-time value would be a
Val::Object
.I don't think there should be any JS-like
this
syntax/semantics. I think we should be running entirely on lexical scoping. That way, people would essentially get private attributes "for free". Since objects are immutable, this would be the only way to get immutability anyway.In other words, there's no such thing as "object methods" (much like JavaScript), and there's no "object context"/
this
(unlike JavaScript). If you want to refer to the object, just assign it (like we did above withobj
) and it'll be available lexically.Accessing properties could probably borrow
obj[key]
— so we do array indexing and object property lookup with the same postfix operator. Or we go the Perl route on that one and haveobj{key}
. I'd be fine either way. I'd be fine with agetProp(obj, key)
setting function too, I think. If we do the postfix lookup, we should consider also having the syntactic sugarobj.key
(like JavaScript); that last one would only work ifkey
is a valid identifier, of course.I'd just like to add that I foresee people wanting to mutate their object, and we should probably supply a
setProp(obj, key, newVal)
setting function for that. Objects stay immutable, but it gets easier to derive new objects from old ones.Also, there should be a
keys(obj)
setting function, returning an array of the key names ofobj
. This function gives no guarantees whatsoever about the order of the keys. There's no big need for aclone
setting function, because objects are immutable.Things we're explicitly not adding/borrowing from ES6 and other languages:
Inheritance__proto__
and prototypesThe text was updated successfully, but these errors were encountered: