associated-types

    0热度

    2回答

    我正在阅读由Apple发布的Swift书。我有XCode 7.2.1 下面的代码给出了一个编译错误。我在游乐场使用了代码。 protocol Container { associatedtype ItemType mutating func append(item: ItemType) var count: Int { get } subscript(i:

    0热度

    2回答

    我试图这样做: protocol HasElement { associatedtype ItemType func getElement() -> ItemType func setElement(element: ItemType) } class Element {} class BarElement: Element {} class Foo: N

    4热度

    2回答

    我想强制关联类型为Self,但编译器没有它。 这里就是我想要去编译: protocol Protocol { // Error: Inheritance from non-protocol, non-class type 'Self' associatedtype Type: Self } 你可能会问,为什么不使用Self代替相关类型的?仅仅因为我不能:关联类型是从父协议

    2热度

    1回答

    我无法了解关联的类型。我的问题代码: trait Fooer { fn foo(&self); } trait FooStore { type T: Fooer; fn store_foo(&self, fooer: Self::T); } #[allow(dead_code)] struct DB {} impl FooStore for DB {

    0热度

    1回答

    当我尝试编译下面的代码时,出现了分段错误。我试图在CellUpdater结构上创建一个类型受限扩展,该结构访问一个属性,该属性的类型是在泛型类型的关联类型上定义的。不知道我是否做错了什么,或者它是否是Swift编译器的限制,有什么想法? protocol CellUpdaterType { func generateDetailsDrillDownController(index: In

    0热度

    1回答

    我尝试获得更通用的东西,但是在尝试使用关联类型时失败。鉴于是Shape特质和Renderer类型,它试图返回一个Builder为特定类型的形状。 形状 #[derive(Clone, Copy)] pub struct Rectangle{ pub origin: Point, pub height: usize, pub width: usize, } pu

    1热度

    1回答

    我有下面的代码: protocol NextType { associatedtype Value associatedtype NextResult var value: Value? { get } func next<U>(param: U) -> NextResult } struct Something<Value>: NextType

    2热度

    2回答

    我有一些实现Resource协议的结构。这定义它们必须具有符合ExtendedInfo协议的变量extendedInfo,以便通过init(json: [String: AnyObject]提供使用json初始化它们的方式。我试图提供一种方法来动态实例化这些,使用JSON,提供正确类型的ExtendedInfo并将其分配给结构的extendedInfo变量。然而,试图通过他们的dynamicTyp

    6热度

    1回答

    我有一个协议我的swift代码库我有协议与关联的类型和两种方法。这两种方法都为协议的关联类型定义了不同的通用约束。我想使结构符合两个协议,但有两种不同的关联类型。 protocol Convertable { associatedtype TargetType func convert() -> TargetType } func show<T : Convertable

    3热度

    1回答

    在Swift 2.3中数组是否定义了元素关联类型? 它必须定义它,因为它实现了具有元素和关联类型的GeneratorType。 Dictionary和Set都定义了元素,但是Array在哪里定义它? 数组有一个称为元素的泛型类型,具有泛型类型是否满足GeneratorType协议? 我在操场上试过这个,它对我没有帮助。 例如 protocol Animal{ associatedtype