2016-08-08 13 views
1

Lassen Sie uns sagen, ich habe ein Protokoll und eine Struktur wie folgt:Ist es möglich, eingebettete Generika in Swift zu konstruieren?

protocol A { 
    var someType: UnrelatedProtocol.Type { get } 
} 

struct B<T: UnrelatedProtocol> { 
    var anotherThing: T? 
} 

Und ich will diese wie folgt zusammen verwenden:

struct C<T: A> { 
    typealias SomeThing = (B<T.someType>) -> Void 
} 

in Swift ist das möglich? Ich habe damit herumgespielt, kann es aber nicht richtig machen. Vielleicht ist es nicht möglich, aber ich habe das Gefühl, ich könnte so etwas machen.

Danke!

Antwort

1

Zunächst einmal haben wir

protocol UnrelatedProtocol { } 

Jetzt müssen wir A

protocol A { 
    associatedtype SomeType: UnrelatedProtocol 
    var someType: SomeType { get } 
} 

Und schließlich

struct B<T: UnrelatedProtocol> { 
    var anotherThing: T? 
} 

struct C<T: A> { 
    typealias Logic = (B<T.SomeType>) ->() 
} 
einen zugehörigen Typ innerhalb des Protokolls definieren
Verwandte Themen