swift - 在 Swift 中,您能否创建一个协议,该协议仅在关联类型的特定条件成立时才需要特定函数?
问题描述
我想表达一个类似于以下两个都无法编译的片段的 Swift 协议。
尝试1:
protocol AbstractFunction {
associatedtype Domain
associatedtype Codomain
func apply(_ x: Domain) -> Codomain
static var identity: Self where Domain == Codomain { get }
}
尝试2:
protocol AbstractFunction {
associatedtype Domain
associatedtype Codomain
func apply(_ x: Domain) -> Codomain
static func identity() -> Self where Domain == Codomain { get }
}
第一个在 Swift 语法中甚至无效,而第二个则以
'where' clause cannot be attached to a non-generic declaration
.
这两个例子都试图表达一个协议,该协议描述不是实际函数类型实例的函数(A) -> B
。如果有类型Vector2
and Vector3
,可以想象创建类型Matrix2x2
,Matrix2x3
和Matrix3x3
并使它们符合AbstractFunction
协议。的域MatrixNxM
是VectorM
,共域是VectorN
。方阵有一个单位矩阵,但是当域和余域不同时,单位矩阵(或真正的单位函数)的概念没有意义。
因此,我希望协议AbstractFunction
要求符合类型以提供身份,但仅在Domain == Codomain
. 这可能吗?
解决方案
您可以通过将第二个更严格的协议声明为:
protocol AbstractFunction {
associatedtype Domain
associatedtype Codomain
func apply(_ x: Domain) -> Codomain
}
protocol AbstractEndofunction: AbstractFunction where Codomain == Domain {
static var identity: Self { get }
}
Int -> Int 函数的示例:
final class IntFunction: AbstractEndofunction {
typealias Domain = Int
static var identity = IntFunction { $0 }
private let function: (Int) -> Int
init(_ function: @escaping (Int) -> Int) {
self.function = function
}
func apply(_ x: Int) -> Int {
return function(x)
}
}
推荐阅读
- axon - 轴突框架中的通用 CommandExecutionException 处理
- laravel - OSX 意外将 localhost 请求转发到域
- arrays - 计算存储在 char 数组中的字符串长度时,“strlen”的奇怪输出
- python - 无法在 python selenium 中为此网站创建动态 xpath
- flutter - 制作搜索页面时不断出错
- linux - 与腻子连接时将文件从服务器下载到 PC 的 Linux shell 脚本
- macos - NSRunningApplication activateWithOptions 方法行为随着 Big Sur 的改变
- javascript - 如何在 Parsley js 错误期间添加类?
- javascript - 在 setState 中使用 ...prev 禁止 userEvent.type 在反应测试时输入输入
- c# - ASP .NET Сore Razor Pages 在请求之间持久化数据