facebook / facebook/hhvm

Support for higher-kinded types

未关闭
#2,825 6 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看
feature request hack wishlist
主要语言
C++
星标
18.7k
派生
3.1k
平均合并
1 小时 47 分钟
30 天内合并 PR
2

描述

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);
}
}
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。