Last active
April 14, 2017 07:14
-
-
Save ninjazoete/037826624266888a73957307106fcc6a to your computer and use it in GitHub Desktop.
Associatedtype as generic constraint
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 Food {} | |
protocol BrazilianFood: Food {} | |
protocol EnglishFood: Food {} | |
class EnglishPancake: EnglishFood {} | |
class BrazilianSoup: BrazilianFood {} | |
protocol Chef { | |
associatedtype FoodType: Food | |
func food<T>() -> T where T : FoodType // <-- error: type 'T' constrained to non-protocol type 'FoodType' | |
} | |
// --- Chefs for each country | |
class EnglishChef: Chef { | |
func food<T>() -> T where T : EnglishFood { | |
} | |
} | |
class BrazilianChef: Chef { | |
func food<T>() -> T where T : BrazilianFood { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment