Embrace Swift generics

RSS for tag

Discuss the WWDC22 Session Embrace Swift generics

Posts under wwdc2022-110352 tag

5 Posts

Post

Replies

Boosts

Views

Activity

Unboxing protocol to concrete type in SwiftUI View
Using Swift 5.7 we're trying to use protocols for testing and mocking in our SwiftUI App. With any and some we're able to hold a heterogeneous list of protocols with self constraints which works perfectly. What we're running into now is that we can't undox the any Protocol into a concrete type for the view. Here's a basic example: protocol ItemProtocol: ObservableObject {     var id: String { get } } struct ListSection {     var id: Int     let title: String     let items: [any ItemProtocol] } protocol ViewModelProtocol: ObservableObject {     var sections: [ListSection] { get } } struct MyView<T: ViewModelProtocol>: View {   @ObservedObject     var viewModel: T     init(viewModel: T) {         self.viewModel = viewModel     }     var body: some View {         List(viewModel.sections, id: \.id) { section in             Section {                 ForEach(section.items, id: \.id) { item in                     RowView(item: item)                     // create view for some ItemProtocol                     Text("Hello Item")                 }             } header: {                 Text(section.title)             }         }     } } struct RowView<T: ItemProtocol>: View {     @ObservedObject     var item: T     init(item: T) {         self.item = item     }     var body: some View {         Text("Row View")     } } This will result in an error: Type 'any ItemProtocol' cannot conform to 'ItemProtocol' I had hoped that the any ItemProtocol would be unboxed to it's concrete type and a concrete type of View would be created.
3
1
2k
Nov ’22
How to check if Data Type equals Generic Data Type and assign to Property if True?
Context I have a Generic Swift Class containing a component Property of the Generic Type. A few other Variables with different Data Types are passed through the Initializer, however, all of them are conforming to the Generic Protocol. However, I get the following Compiler Error in Line 11: 'ComponentA' is not convertible to 'C' Code protocol Component { ... } struct ComponentA: Component { ... } struct ComponentB: Component { ... } class Main&lt;C: Component&gt; { var component: C init(componentA: ComponentA, componentB: ComponentB) { // I am trying to check, whether componentA equals the Generic Data Type and assign it to the component Property if true. if case let safeComponent = componentA as C { self.component = safeComponent } } } Question How can I achieve my goal of checking whether a Data Type equals the Generic Data Type and assign it to the component Property if true?
1
0
942
Aug ’22
Some Animal and local scope of underlying type
Trying to understand why my Xcode-beta 2 is not able to understand the underlying type for the following example /// Protocol that has the idea of draw protocol Drawable {     func draw() } /// Implementation of a circle which is drawable struct Circle: Drawable {     func draw() {         print("Circle")     } } /// Implementation of a Triangle which is drawable struct Triangle: Drawable {     func draw() {         print("Triangle")     } } /// Let make an instance as shown in the example in the `Embrace Swift Generics` Talk func underlyingTypeDoesNotChangeWithinTheScop() {     var drawable: some Drawable = Circle()     drawable.draw()     drawable = Circle() /// 👈🏽 This line should compile, since the underlying type does not change within the scope     drawable.draw() } Why am I getting the following error cannot assign value of type 'Circle' to type 'some Drawable' when I try to assign Circle to var drawable, from what I understand the underlying type remains the same within the scope. Is this an Xcode14-beta 2 issue?
4
0
794
Jul ’22
Generics and AssociatedTypes
Sorry, I did not have a better title for this, I hope it gets clearer with code. The idea is to build a simple facade for Persistent Storage of some objects: class PersistantStorage<T, Codable, Identifiable> {     func store(_ object: T) throws { }          func objectFor(_ key: T.ID) -> T? {         return nil     } } As apparent, there is the generic type T, which is constrained to Codable and Identifiable. Now I want to use the later constraint to define my objectFor method, but the compiler complains: 'ID' is not a member type of type 'T' How would I do this? Or is this completely the wrong approach? Thanks Alex
1
0
1.6k
Jun ’22
Unboxing protocol to concrete type in SwiftUI View
Using Swift 5.7 we're trying to use protocols for testing and mocking in our SwiftUI App. With any and some we're able to hold a heterogeneous list of protocols with self constraints which works perfectly. What we're running into now is that we can't undox the any Protocol into a concrete type for the view. Here's a basic example: protocol ItemProtocol: ObservableObject {     var id: String { get } } struct ListSection {     var id: Int     let title: String     let items: [any ItemProtocol] } protocol ViewModelProtocol: ObservableObject {     var sections: [ListSection] { get } } struct MyView<T: ViewModelProtocol>: View {   @ObservedObject     var viewModel: T     init(viewModel: T) {         self.viewModel = viewModel     }     var body: some View {         List(viewModel.sections, id: \.id) { section in             Section {                 ForEach(section.items, id: \.id) { item in                     RowView(item: item)                     // create view for some ItemProtocol                     Text("Hello Item")                 }             } header: {                 Text(section.title)             }         }     } } struct RowView<T: ItemProtocol>: View {     @ObservedObject     var item: T     init(item: T) {         self.item = item     }     var body: some View {         Text("Row View")     } } This will result in an error: Type 'any ItemProtocol' cannot conform to 'ItemProtocol' I had hoped that the any ItemProtocol would be unboxed to it's concrete type and a concrete type of View would be created.
Replies
3
Boosts
1
Views
2k
Activity
Nov ’22
How to check if Data Type equals Generic Data Type and assign to Property if True?
Context I have a Generic Swift Class containing a component Property of the Generic Type. A few other Variables with different Data Types are passed through the Initializer, however, all of them are conforming to the Generic Protocol. However, I get the following Compiler Error in Line 11: 'ComponentA' is not convertible to 'C' Code protocol Component { ... } struct ComponentA: Component { ... } struct ComponentB: Component { ... } class Main&lt;C: Component&gt; { var component: C init(componentA: ComponentA, componentB: ComponentB) { // I am trying to check, whether componentA equals the Generic Data Type and assign it to the component Property if true. if case let safeComponent = componentA as C { self.component = safeComponent } } } Question How can I achieve my goal of checking whether a Data Type equals the Generic Data Type and assign it to the component Property if true?
Replies
1
Boosts
0
Views
942
Activity
Aug ’22
Having problems trying out the example code in this session
I've copied the code as the video was coming along, and getting this error: error: type 'any Animal' cannot conform to 'Animal' note: only concrete types such as structs, enums and classes can conform to protocols note: required by instance method 'feed' where 'some Animal' = 'any Animal' is this functionality not yet available in Xcode 14b1?
Replies
9
Boosts
1
Views
2.6k
Activity
Aug ’22
Some Animal and local scope of underlying type
Trying to understand why my Xcode-beta 2 is not able to understand the underlying type for the following example /// Protocol that has the idea of draw protocol Drawable {     func draw() } /// Implementation of a circle which is drawable struct Circle: Drawable {     func draw() {         print("Circle")     } } /// Implementation of a Triangle which is drawable struct Triangle: Drawable {     func draw() {         print("Triangle")     } } /// Let make an instance as shown in the example in the `Embrace Swift Generics` Talk func underlyingTypeDoesNotChangeWithinTheScop() {     var drawable: some Drawable = Circle()     drawable.draw()     drawable = Circle() /// 👈🏽 This line should compile, since the underlying type does not change within the scope     drawable.draw() } Why am I getting the following error cannot assign value of type 'Circle' to type 'some Drawable' when I try to assign Circle to var drawable, from what I understand the underlying type remains the same within the scope. Is this an Xcode14-beta 2 issue?
Replies
4
Boosts
0
Views
794
Activity
Jul ’22
Generics and AssociatedTypes
Sorry, I did not have a better title for this, I hope it gets clearer with code. The idea is to build a simple facade for Persistent Storage of some objects: class PersistantStorage<T, Codable, Identifiable> {     func store(_ object: T) throws { }          func objectFor(_ key: T.ID) -> T? {         return nil     } } As apparent, there is the generic type T, which is constrained to Codable and Identifiable. Now I want to use the later constraint to define my objectFor method, but the compiler complains: 'ID' is not a member type of type 'T' How would I do this? Or is this completely the wrong approach? Thanks Alex
Replies
1
Boosts
0
Views
1.6k
Activity
Jun ’22