Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eonist/cdd5afd87f4708c1a184a6e725b26c6a to your computer and use it in GitHub Desktop.
Save eonist/cdd5afd87f4708c1a184a6e725b26c6a to your computer and use it in GitHub Desktop.
A swift protocol with associated type used as type parameter in generic function
protocol Thing {
typealias argType
func doit(val:argType) -> argType
}
class IntThing : Thing {
func doit(val: Int) -> Int {
return val + 1
}
}
func doThing<A:Thing>(thing:A, val:A.argType) -> A.argType {
return thing.doit(val)
}
doThing(IntThing(), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment