Swift-协议中与where子句相关的类型? [英] Swift - associated types in protocol with where clause?

查看:359
本文介绍了Swift-协议中与where子句相关的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

protocol SomeProtocol {
  typealias F: Foo
  typealias FB: FooBar where FB.Foo == F
}

但是这不能编译,因为我们不能像这样将where子句放在typealias中.

But this doesn't compile since we cannot put where clause in typealias like that.

我必须在这里丢失一些东西,因为可以使用type parameterization这样轻松地做到这一点:

I must be missing something here since this can be easily done with type parameterization like this:

struct SomeStruct<F: Foo, FB: FooBar where FB.Foo == F> {}

associated type where子句是什么?

推荐答案

Swift(2.1)当前不支持协议中关联类型的类型参数化.

Type parameterization of associated types in protocols is currently not supported in Swift (2.1).

尽管在这种情况下,您甚至不需要where子句来实现功能.您可以从中获得更多的方便:

Although in this case you don't even need the where clause for functionality. It's more the convenience you get where you can do this:

func someFunc<T: SomeProtocol>(someProt: T, foo: T.F) {
    ...
}

// Instead of this:

func someFunc<T: SomeProtocol>(someProt: T, foo: T.FB.Foo) {
    ...
}

这篇关于Swift-协议中与where子句相关的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆