必须声明类型别名 public 因为它符合 public 协议中的要求

Typealias must be declared public because it matches a requirement in public protocol

我正在实施一个基本的 Functor

// class Functor f where
public protocol Functor {
    typealias A
    typealias B
    typealias FB

    // fmap :: (a -> b) -> f a -> f b
    func fmap(f: A -> B) -> FB
}

public struct Box<X> {
    let value: X

    public init(value v: X) {
        value = v
    }
}

extension Box : Functor {
    public typealias A = X
    public typealias B = Any
    public typealias FB = Box<B>

    public func fmap(f: A -> B) -> FB {
        return FB(value: f(value))
    }
}

如果我不声明public typealias,会弹出一个错误

Typealias must be declared public because it matches a requirement in public protocol

该消息没有提供太多线索,为什么需要 public?

协议的所有成员必须具有与该协议相同的访问权限。由于此要求,所有成员都隐式设置为相同的访问级别。

你的 typealias 问题一定是一个错误 Swift。 typealias 应该隐含地是 public,就像 func fmap(_) 是 public.

Access Control

The access level of each requirement within a protocol definition is automatically set to the same access level as the protocol. You cannot set a protocol requirement to a different access level than the protocol it supports. This ensures that all of the protocol’s requirements will be visible on any type that adopts the protocol.

NOTE

If you define a public protocol, the protocol’s requirements require a public access level for those requirements when they are implemented. This behavior is different from other types, where a public type definition implies an access level of internal for the type’s members.