Proposal: Tagged Constructor & Taggable Function/Map/Set constructor

# Sm In (5 months ago)

Template literal(includes tagged template) is so strong feature. It makes it possible to add elegant DSL for Library(or maybe framework) which also can interact with js.(styled-component proofed this with their api) Why don't we use them to enhance ECMAScript's API?

If we allow using tagged template to constructors just like this:

new Constructor`
  blahlah
`; // what about to call this syntax as tagged constructor?

we can do something like this:

If Function construcor is taggable

const sum = new Function`
  (a, b) {
    return a + b;
  }
`;

If Map constructor is taggable

new Map`
   a: 1234,
   one: ${() => 1},
   str: 'Hello!',
   bool: true
`;

If Set constructor is taggable. and It supports list comprehension

new Set`
  ${range}
    -> ${x => x * 2}
    -> ${x => x < 10}
`; // Set [2, 4, 6, 8]

Feedback is welcome!

# Richard Gibson (5 months ago)

That syntax is already valid, and has a different meaning. new ctorFactory first evaluates the tagged template (invoking ctorFactory with arguments derived from the template literal) and then invokes that result as a constructor. For example, (new ctorFactoryreturn function Ctor(){}).name equals "Ctor". You could get clever with Reflect.construct or build an interpreter for using a domain-specific language as input ( example michaelfig/jessica/blob/8832590eb988a9dfafa0d6ab112c97bacb970ac8/lib/quasi-peg.mjs#L52),

but I don't think there are many other options.

# Sm In (5 months ago)

Thank you for your feedback! I just found that this would be great if it is implemented as library as you suggested. :)