如何快速为HKT声明协议?

dua*_*uan 5 haskell functional-programming swift

在 Haskell 中,使用 时typeclass,很容易声明其“实例”类型类型的约束。

class Functor (f :: * -> *) where
  ...
Run Code Online (Sandbox Code Playgroud)

* -> *代表 HKT(Higher-Kinded Types),这意味着任何符合的类型都Functor必须是 HKT。

我怎样才能用 Swift 实现这一点protocol

dua*_*uan 5

Swift 本身不支持 HKT 作为类型形式,但您可以使用以下技巧模拟约束associatedtype

public protocol Functor {
    /// (* -> *)
    associatedtype FA: Functor = Self
    /// *
    associatedtype A
    /// fmap
    func fmap<B>(_ f: ((A) -> B)) -> FA where FA.A == B
}
Run Code Online (Sandbox Code Playgroud)

和一致性示例:

enum Maybe<A> {
    case just(A)
    case nothing
}

extension Maybe: Functor {
    func fmap<B>(_ transform: ((A) -> B)) -> Maybe<B> {
        switch self {
        case .nothing:
            return .nothing
        case .just(let v):
            return .just(transform(v))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)