dynamic class idiom
On Aug 26, 2008, at 2:49 PM, Dave Herman wrote:
I noticed a little idiom supported by classes-as-values:
(new (class() { ... }))
This essentially builds an object whose properties can be computed
in an arbitrary statement context-- with loops or what have you. You might call this a "dynamic singleton" pattern; a throwaway factory. It's
not a global singleton like in Java; it's just an ordinary expression so it might be called by a function or whatever.
Probably everyone involved favors first-class classes -- first-class
meaning classes as values (not only as named defining forms),
nestable. I hoped so in reply to Peter yesterday.
Worth a little sugar? E.g.:
object { ... } ~=~ (new (class() { ... }))
Does not say class, seems to save only a few chars (you over- parenthesize new ;-). Wait for demand?
or maybe to conserve keywords (a bit backwards-incompatible):
new { ... } ~=~ (new (class() { ... }))
This is not incompatible at all, since an object initialiser has no
[[Construct]] in ES3, so cannot be the operand of new.
Does not say class, seems to save only a few chars (you over-parenthesize new ;-). Wait for demand?
It's not about saving chars so much as introducing a new (lightweight) semantic concept: an object initializer that is a block rather than a table mapping names to expressions. Not saying `class' is the point.
I over-parenthesized because I wasn't sure how it would parse if you said e.g.
new class() { ... }.foo
or maybe to conserve keywords (a bit backwards-incompatible):
new { ... } ~=~ (new (class() { ... }))
This is not incompatible at all, since an object initialiser has no [[Construct]] in ES3, so cannot be the operand of new.
Right, but that means that existing code isn't using this form, so you could steal this special case; then the argument to `new' would have to be an expression that didn't start with '{', just like expression statements. It's backwards-incompatible but since the current syntax only leads to a useless error, I doubt real code is using it.
On Aug 26, 2008, at 4:16 PM, Dave Herman wrote:
Does not say class, seems to save only a few chars (you over-parenthesize new ;-). Wait for demand?
It's not about saving chars so much as introducing a new (lightweight) semantic concept: an object initializer that is a block rather than a table mapping names to expressions. Not saying `class' is the point.
But you'd say class if you needed to define the operand to new for
other uses, or for some other reason motivating against this
"lightweight" form. So the concept seems to have two names when
generalized slightly.
Specializations can have special names, don't get me wrong. But
object is way overloaded.
I over-parenthesized because I wasn't sure how it would parse if you said e.g.
new class() { ... }.foo
That should be unambiguous, and work as this does in ES1-3:
new MyClass(arg).foo
The grammar goes to some lengths to group (new MyClass(arg)).
or maybe to conserve keywords (a bit backwards-incompatible):
new { ... } ~=~ (new (class() { ... }))
This is not incompatible at all, since an object initialiser has no [[Construct]] in ES3, so cannot be the operand of new.
Right, but that means that existing code isn't using this form, so you could steal this special case; then the argument to `new' would
have to be an expression that didn't start with '{', just like expression statements. It's backwards-incompatible but since the current syntax only leads to a useless error, I doubt real code is using it.
We consider such guaranteed errors to be unused, and call changes to
claim the syntax backward-compatbile. At least we did in the ES4 WG.
ES3 chapter 16 allows implementations to extend in this way, too.
I noticed a little idiom supported by classes-as-values:
This essentially builds an object whose properties can be computed in an arbitrary statement context-- with loops or what have you. You might call this a "dynamic singleton" pattern; a throwaway factory. It's not a global singleton like in Java; it's just an ordinary expression so it might be called by a function or whatever.
Worth a little sugar? E.g.:
or maybe to conserve keywords (a bit backwards-incompatible):