-
-
Save sturdysturge/57299c81278adfe03cbb825107d53310 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| protocol MyProtocol { | |
| associatedtype MyAssociatedType: StringProtocol | |
| var myAssociatedType: MyAssociatedType { get } | |
| } | |
| struct MyConformingType: MyProtocol { | |
| var myAssociatedType = "Hello" | |
| } | |
| struct MyOtherConformingType: MyProtocol { | |
| var myAssociatedType = "World" | |
| } | |
| struct MyType { | |
| var anyExample: any MyProtocol = MyConformingType() | |
| var someExample: some MyProtocol = MyConformingType() | |
| } | |
| struct ContentView: View { | |
| @State var myType = MyType() | |
| var body: some View { | |
| VStack { | |
| Text("myType.anyExample: " + String(describing: type(of: myType.anyExample))) | |
| Text("myType.someExample: " + String(describing: type(of: myType.someExample))) | |
| Button("Change") { | |
| myType.anyExample = MyOtherConformingType() | |
| // Will not compile as type is not 'some MyProtocol' | |
| // myType.someExample = MyOtherConformingType() | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment