-
-
Save krzyzanowskim/e70cef7c3bf7f389c9f48eccb1806219 to your computer and use it in GitHub Desktop.
This file contains 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
protocol Theme { | |
associatedtype Kind | |
} | |
struct Theme1: Theme { | |
typealias Kind = String | |
let kind: Kind | |
} | |
let theme1 = Theme1(kind: "abc") | |
struct Theme2: Theme { | |
typealias Kind = Int | |
let kind: Kind | |
} | |
let theme2 = Theme2(kind: 999) // OK | |
// -------------------------- | |
func current1() -> Theme1 { | |
return theme1 | |
} | |
print(current1()) // OK | |
// -------------------------- | |
func current2<T>() -> T { | |
return theme2 as! T | |
} | |
print(current2()) // OK | |
// -------------------------- | |
func current3<T: Theme>() -> T { | |
return theme2 as! T | |
} | |
print(current3() as Theme2) // OK | |
// -------------------------- | |
func current4<T: Theme>() -> T { | |
return theme2 // of type `Theme2` | |
} | |
print(current4()) // error: generic parameter 'T' could not be inferred | |
// -------------------------- | |
func current5<T>() -> T where T: Theme, T.Kind == String { | |
return theme2 // of type `Theme2` | |
} | |
print(current5(Theme2.self)) // error: generic parameter 'T' could not be inferred | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment