dynamic class idiom

Dave Herman dherman at ccs.neu.edu
Tue Aug 26 14:49:11 PDT 2008


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.

Worth a little sugar? E.g.:

     object { ... } ~=~ (new (class() { ... }))

or maybe to conserve keywords (a bit backwards-incompatible):

     new { ... } ~=~ (new (class() { ... }))

Dave


More information about the Es-discuss mailing list