Skip to content

Instantly share code, notes, and snippets.

@ninjazoete
Last active April 14, 2017 07:14
Show Gist options
  • Save ninjazoete/037826624266888a73957307106fcc6a to your computer and use it in GitHub Desktop.
Save ninjazoete/037826624266888a73957307106fcc6a to your computer and use it in GitHub Desktop.
Associatedtype as generic constraint
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