Post

Replies

Boosts

Views

Activity

Please help me understand “Finding the Dynamic Type in a Generic Context”
Please help me understand this section! I'm reading Finding the Dynamic Type in a Generic Context that has this snippet: func printGenericInfo<T>(_ value: T) { let t = type(of: value) print("'\(value)' of type '\(t)'") } protocol P {} extension String: P {} let stringAsP: P = "Hello!" printGenericInfo(stringAsP) // 'Hello!' of type 'P' ... that's followed up by this sentence: This unexpected result occurs because the call to type(of: value) inside printGenericInfo(_:) must return a metatype that is an instance of T.Type , but String.self (the expected dynamic type) is not an instance of P.Type (the concrete metatype of value). 1. How come String.self is not an instance of P when I can run this code? func f(_ t: P.Type) { print("...") } f(String.self) 2. Why does type(of:) return the concrete metatype outside but not inside generic functions? print("'\(stringAsP)' of type '\(type(of: stringAsP))'") // 'Hello!' of type 'String'
1
0
467
Feb ’24
What is the name of this technique/pattern?
I notice that this pattern shows up quite frequently in the Swift world but I don't know what its name is. define a new type that conforms to a protocol solely use the new type as a unique value to create a new "thing" extension VerticalAlignment { struct CustomAlignment: AlignmentID {...} static let customAlignment = VerticalAlignment(CustomAlignment.self) }
0
0
398
Feb ’24
Please help me understand “Finding the Dynamic Type in a Generic Context”
Please help me understand this section! I'm reading Finding the Dynamic Type in a Generic Context that has this snippet: func printGenericInfo<T>(_ value: T) { let t = type(of: value) print("'\(value)' of type '\(t)'") } protocol P {} extension String: P {} let stringAsP: P = "Hello!" printGenericInfo(stringAsP) // 'Hello!' of type 'P' ... that's followed up by this sentence: This unexpected result occurs because the call to type(of: value) inside printGenericInfo(_:) must return a metatype that is an instance of T.Type , but String.self (the expected dynamic type) is not an instance of P.Type (the concrete metatype of value). 1. How come String.self is not an instance of P when I can run this code? func f(_ t: P.Type) { print("...") } f(String.self) 2. Why does type(of:) return the concrete metatype outside but not inside generic functions? print("'\(stringAsP)' of type '\(type(of: stringAsP))'") // 'Hello!' of type 'String'
Replies
1
Boosts
0
Views
467
Activity
Feb ’24
What is the name of this technique/pattern?
I notice that this pattern shows up quite frequently in the Swift world but I don't know what its name is. define a new type that conforms to a protocol solely use the new type as a unique value to create a new "thing" extension VerticalAlignment { struct CustomAlignment: AlignmentID {...} static let customAlignment = VerticalAlignment(CustomAlignment.self) }
Replies
0
Boosts
0
Views
398
Activity
Feb ’24