question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow classes to be parametric in other parametric classes

See original GitHub issue

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

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

interface Cartesian<T<~>> {
  all<A>(a: Array<T<A>>): T<Array<A>>;
}

Parametric type parameters can take any number of arguments:

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:

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 generics in this way (see issue 308):

typedef Maybe<Array<~>> Composite<~> ;
class Foo implements Monad<Composite<~>> { ... }

The arities of the definition and the alias must match for the typedef to be valid.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:1056
  • Comments:174 (35 by maintainers)

github_iconTop GitHub Comments

146reactions
zpdDG4gta8XKpMCdcommented, Jun 6, 2016

with HKT’s mindsets can be changed, habits broken, lost generations brought back to life, it would the biggest thing since generics and explicit nulls and undefineds, it can change everything

please consider it as a next big feature, stop listen to people who keep asking you for a better horse, give them a f***g ferrari

132reactions
agyemanjpcommented, Aug 3, 2020

Still no update? In my opinion, this issue ranks as the number one obstacle to TypeScript achieving its full potential. There are so many instances where I try to type my libraries properly, only to give up after a long struggle, realizing that I have run up against this limitation again. It is pervasive, showing up even in seemingly very simple scenarios. Really hope it will be addressed soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parametric class in Java - Stack Overflow
I have 2 classes, one is the Main class and the other is the parametric class. When I create a new Object with...
Read more >
Parametric Type Classes
The class parameter types, on the other hand, depend on the placeholder type. One consequence of these restrictions is that there is at...
Read more >
SystemVerilog Parameterized Classes - ChipVerify
Learn about SystemVerilog parameterized classes, how to define and write, pass a different parameter and more - SystemVerilog Tutorial for Beginners.
Read more >
Parametric types - CS 242
Objects are functions and data grouped together into one value. Consider that when defining a class (a type), methods are allowed to reference...
Read more >
Calculus II - Parametric Equations and Curves
We will graph several sets of parametric equations and discuss how to ... give the direction while in other cases either one could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found