Allow classes to be parametric in other parametric classes
This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose writing ``` javascript interface Monad<T<~>> { map<A, B>(f: (a: A) => B): T<A> => T<B>; lift<A>(a: A): T<A>; join<A>(tta: T<T<A>>): T<A>; } ``` Similarly, it's possible to write specific examples of cartesian functors, but in order to write the interface that all cartesian functors satisfy, I propose writing ``` javascript interface Cartesian<T<~>> { all<A>(a: Array<T<A>>): T<Array<A>>; } ``` Parametric type parameters can take any number of arguments: ``` javascript interface Foo<T<~,~>> { bar<A, B>(f: (a: A) => B): T<A, B>; } ``` That is, when a type parameter is followed by a tilde and a natural arity, the type parameter should be allowed to be used as a generic type with the given arity in the rest of the declaration. Just as is the case now, when implementing such an interface, the generic type parameters should be filled in: ``` javascript class ArrayMonad<A> implements Monad<Array> { map<A, B>(f: (a:A) => B): Array<A> => Array<B> { return (arr: Array<A>) => arr.map(f); } lift<A>(a: A): Array<A> { return [a]; } join<A>(tta: Array<Array<A>>): Array<A> { return tta.reduce((prev, cur) => prev.concat(cur)); } } ``` In addition to directly allowing compositions of generic types in the arguments, I propose that typedefs also support defining gener
Posted by metaweta on Nov 19, 2014, 6:46 PM
Tags
Related issues
- The only thing keeping me on VSCode these days is the remote SSH development.
- Something similar was done in a sheep model: "A bioactive supramolecular and covalent polymer scaffold for cartilage repair in a sheep m...
- Proposal for OpenHarmony Adaptation of [ ThreadPool ]
- Proposal for a pipe-like operator to chain constructor/method invocations without nesting
- Implement support for Node Stashing
- RSC and CDN interaction makes next.js inefficient for highload projects
Raise real-world problems, find people facing the same issue, and discuss solutions.9,000 issues0 comments
support (E-commerce)
The only thing keeping me on VSCode these days is the remote SSH development.
Something similar was done in a sheep model: "A bioactive supramolecular and covalent polymer scaffold for cartilage repair in a sheep m...
Proposal for OpenHarmony Adaptation of [ ThreadPool ]
Proposal for a pipe-like operator to chain constructor/method invocations without nesting
Implement support for Node Stashing
RSC and CDN interaction makes next.js inefficient for highload projects
Extract Action Notifier framework for push notifications
Improve support for monorepo by supporting git options such as depth, and sparse checkout
Allow classes to be parametric in other parametric classes
This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose writing ``` javascript interface Monad<T<~>> { map<A, B>(f: (a: A) => B): T<A> => T<B>; lift<A>(a: A): T<A>; join<A>(tta: T<T<A>>): T<A>; } ``` Similarly, it's possible to write specific examples of cartesian functors, but in order to write the interface that all cartesian functors satisfy, I propose writing ``` javascript interface Cartesian<T<~>> { all<A>(a: Array<T<A>>): T<Array<A>>; } ``` Parametric type parameters can take any number of arguments: ``` javascript interface Foo<T<~,~>> { bar<A, B>(f: (a: A) => B): T<A, B>; } ``` That is, when a type parameter is followed by a tilde and a natural arity, the type parameter should be allowed to be used as a generic type with the given arity in the rest of the declaration. Just as is the case now, when implementing such an interface, the generic type parameters should be filled in: ``` javascript class ArrayMonad<A> implements Monad<Array> { map<A, B>(f: (a:A) => B): Array<A> => Array<B> { return (arr: Array<A>) => arr.map(f); } lift<A>(a: A): Array<A> { return [a]; } join<A>(tta: Array<Array<A>>): Array<A> { return tta.reduce((prev, cur) => prev.concat(cur)); } } ``` In addition to directly allowing compositions of generic types in the arguments, I propose that typedefs also support defining gener
Comments (0)
No comments yet. Start the discussion.