Last active
April 28, 2022 20:55
-
-
Save troystribling/26cf6a0510454eda1af0 to your computer and use it in GitHub Desktop.
A swift protocol with associated type used as type parameter in generic function
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 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
How would you declare a delegate property of type
Thing
?weak var delegate: Thing?
would produce Xcode error:Protocol 'Thing' can only be used as a generic constraint because it has Self or associated type requirements