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

Richard Gibson richard.gibson at gmail.com
Sun Mar 10 15:20:31 UTC 2019


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 ctorFactory`return 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
<https://github.com/michaelfig/jessica/blob/8832590eb988a9dfafa0d6ab112c97bacb970ac8/lib/quasi-peg.mjs#L52>),
but I don't think there are many other options.

On Sun, Mar 10, 2019 at 1:39 AM Sm In <sbshw1 at gmail.com> wrote:

> 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](https://www.styled-components.com/))
> Why don't we use them to enhance ECMAScript's API?
>
> If we allow using tagged template to constructors just like this:
>
> ```js
> new Constructor`
>   blahlah
> `; // what about to call this syntax as tagged constructor?
> ```
>
> we can do something like this:
>
> #### If `Function` construcor is taggable
>
> ```js
> const sum = new Function`
>   (a, b) {
>     return a + b;
>   }
> `;
> ```
>
> #### If `Map` constructor is taggable
>
> ```js
> new Map`
>    a: 1234,
>    one: ${() => 1},
>    str: 'Hello!',
>    bool: true
> `;
> ```
>
> #### If `Set` constructor is taggable. and It supports list comprehension
>
> ```js
> new Set`
>   ${range}
>     -> ${x => x * 2}
>     -> ${x => x < 10}
> `; // Set [2, 4, 6, 8]
> ```
>
> Feedback is welcome!
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190310/7b8c1950/attachment.html>


More information about the es-discuss mailing list