Support for higher-kinded types
- Lenguaje dominante
- C++
- Estrellas
- 18.7k
- Forks
- 3.1k
- Merge medio
- 1 h 47 min
- PR fusionados (30 d)
- 2
Descripción
Bit of a blue-sky feature request, but I was just wondering if Hack might ever support higher-kinded types? Since we already have a good lambda syntax and type inference, higher-kinded types would put Hack almost on a par with Scala for hybrid functional/object-oriented programming, but with a more familiar syntax for PHP/Java/C# programmers.
The only syntax change I envisage it requiring is a wildcard for generic types. Since types are erased at runtime, it wouldn't require any runtime changes (other than to strip the new syntax), however the type-checker would be (much) harder...! It would allow us to do some pretty fancy stuff with container types, even down to defining a type-safe monad. I envisage it looking something like this:
``` php
> {
public abstract function flatMap((function(T): TMonad) $f): TMonad;
public static abstract function unit(TTo $x): TMonad;
public function map((function(T): TTo) $f): TMonad {
return $this->flatMap($x ==> static::unit($f($x)));
}
}
final class Maybe {
use Monad>;
// null indicates an "empty" maybe
private function __construct(private ?T $value) {}
public abstract function flatMap((function(T): Maybe) $f): Maybe {
return $this->value === null ? Maybe::none() : $f($this->value);
}
public static function unit(TTo $x): Maybe {
return Maybe::just($x);
}
public static function just(TTo $x): Maybe {
return new Maybe($x);
}
public static function none(): Maybe {
// We could optimise this using UNSAFE to have a single none instance...
return new Maybe(null);
}
}
final class MonadOps {
public static function lift, T, TTo>((function(T): TTo) $f): (function(TMonad): TMonad) {
return $x ==> $x->map($f);
}
}
```
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.