Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created December 11, 2018 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/e70cef7c3bf7f389c9f48eccb1806219 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/e70cef7c3bf7f389c9f48eccb1806219 to your computer and use it in GitHub Desktop.
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