Proposal: Class Templates

T.J. Crowder tj.crowder at farsightsoftware.com
Wed Jan 16 15:53:59 UTC 2019


On Wed, Jan 16, 2019 at 1:01 PM ViliusCreator
<viliuskubilius416 at gmail.com> wrote:
>
> Yes, you can do that. But then doesn’t that look ugly?
>
> `new (Abc(1, 2, 3)(4, 5, 6)`, vs `new Abc<4, 5, 6>(1, 2, 3)`.

You meant `new (Abc(4, 5, 6))(1, 2, 3)` vs. `new Abc<4, 5, 6>(1, 2, 3)`,
right? There's not a *lot* in it... :-)

The problem you face is that `new Abc<4>(1, 2, 3)` (where there's only one
thing within the <>, presumably a common case) is already valid syntax:

* `new Abc<4>(1, 2, 3)` becomes
* `o<4>(1, 2, 3)` becomes
* `b1>(1, 2, 3)` becomes
* `b1>3` becomes
* `b2`

...where `o` is the object created by `new Abc` (parens are optional in
`new` if there are no arguments), `b1` is the boolean resulting from `o<4`,
and `b2` is the boolean resulting from `b1>3`.

Even though nearly-nonsensical, assigning valid syntax new meaning is a
very high barrier to jump. (Interestingly, if *weren't* the case that `<`
after `new Identifier` is already valid syntax, the spec changes to enable
making `new Abc<x>` call Abc with x and then use new on the result [no need
to tie it specifically to some "class templates" concept] would be fairly
small [tagged templates already work that way]. But...) You'd need `new
Abc<|4|>` or similar, and...that seems like it's unlikely to happen.

I want to reach for tag functions here, but it would be either **ugly**:

```js
new Abc`${4}${5}${6}`(1, 2, 3)
```

...or an awful and limited hack...

```js
new Abc`4, 5, 6`(1, 2, 3)
```

(parsing the "arguments" in the template from the strings array passed to
the tag function).

`new (Abc(4, 5, 6))(1, 2, 3)` starts looking pretty good. :-)

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190116/ccaee65b/attachment.html>


More information about the es-discuss mailing list