将通用启动参数限制为类通用类型
我正在尝试为协议实现类型的擦除包装器。我的协议具有约束至caseiterable
的相关类型,并且使用该类型的方法。
给定以下定义:
protocol Foo {
associatedtype A: CaseIterable
func doSomething(input: A)
}
final class AnyFoo<A: CaseIterable>: Foo {
private let _doSomething: (A) -> Void
init<Other: Foo>(wrappedFoo: Other) where Other.A == A {
// "Cannot assign value of type '(Other.A) -> ()' to type '(A) -> Void'"
_doSomething = wrappedFoo.doSomething(input:)
}
func doSomething(input: A) {
_doSomething(input)
}
}
我得到了错误无法分配类型的值'(other.a) - &gt; ()'tote'(a) - &gt;在类的初始化器中void'
。似乎编译器将A
和ether.a
作为不同类型解释,我不知道为什么,因为初始化器已限制了ether.a
/code>和
a
是相同的。如果我将caseiterable
替换为hashable
,则没有问题。
有人可以解释为什么会发生这种情况吗?
I'm trying to implement a type erasing wrapper for a protocol. I have a protocol with an associatedtype constrainted to CaseIterable
and a method using that type.
Given the following definitions:
protocol Foo {
associatedtype A: CaseIterable
func doSomething(input: A)
}
final class AnyFoo<A: CaseIterable>: Foo {
private let _doSomething: (A) -> Void
init<Other: Foo>(wrappedFoo: Other) where Other.A == A {
// "Cannot assign value of type '(Other.A) -> ()' to type '(A) -> Void'"
_doSomething = wrappedFoo.doSomething(input:)
}
func doSomething(input: A) {
_doSomething(input)
}
}
I'm getting the error Cannot assign value of type '(Other.A) -> ()' to type '(A) -> Void'
in the initializer of the class. It seems that the compiler interprets A
and Other.A
as different types, and I can't figure out why, because the initializer has constrained Other.A
and A
to be the same. If I replace CaseIterable
with Hashable
, there's no problem.
Can somebody explain why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然你不能。
_dosomething
class yyfoo 是block> block
的类型,它是匿名函数。因此,您可以为其分配wrappedfoo.dosomething(input:)
。当您调用_dosomething(Input)
时,它实际上调用wrappedfoo.dosothing(输入:输入)
。从另一种方式上,您可以定义一个
block
喜欢:但是您不能像:
Of course you can't. The
_doSomething
in classAnyFoo
is type ofBlock
which is an Anonymous function. So you can assignwrappedFoo.doSomething(input:)
to it. When you call_doSomething(input)
, it actually callwrappedFoo.doSomething(input: input)
.In another way, you can define a
Block
like:but you can't define it like: