Proposal: Class Templates

Augusto Moura augusto.borgesm at gmail.com
Wed Jan 16 16:40:53 UTC 2019


In the proposal:
> ``` js
> class SizedArray<size> extends Array {
>
>   constructor(...args) {
>     if(args.length > size) throw new SyntaxError('Argument size is too big.')
>     super(...args)
>   }
>   push(i) {
>     if(this.length + 1 > size) throw new SyntaxError('Cannot push items anymore, array too big.')
>     super.push(i)
>   }
> }
> ```
> Usage:
> Usage is simple, and obvious.
> ``` js
> // ...
> let somethingElse: A<Number> = new A<Number>;
> ```

I don't think the syntax is obvious. What will happen in the if inside
the constructor? What value `size` would receive? Infinity? undefined?
The `Number` function?
That's no way templates will ever work like that in Javascript, it
doesn't makes any sense, C++ templates were implemented to support
generic static allocation and safe typings, neither of this features
applies to Javascript (and probably never will). Your example could
easily be written with constructor arguments (like any sane OOP
language), that's not a problem to be solved here

``` js
class SizedArray extends Array {
  constructor(size, ...args) {
    if (args.length > size) throw Error();
    super(args);
  }
}
```

Em ter, 15 de jan de 2019 às 17:01, IdkGoodName Vilius
<viliuskubilius416 at gmail.com> escreveu:
>
> See: https://github.com/CreatorVilius/ecmascript-proposals/blob/master/proposal-class-templates.md
>
>
>
> I think having Class Templates in JavaScript would be awesome thing and should be implemented. But that’s just my opinion.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



-- 
Atenciosamente,

Augusto Borges de Moura


More information about the es-discuss mailing list