RosettaCodeData/Task/Constrained-genericity/Morfa/constrained-genericity-1.morfa

28 lines
476 B
Plaintext

import morfa.type.traits;
template < T >
alias IsEdible = HasMember< T, "eat" >;
template < T >
if (IsEdible< T >)
struct FoodBox
{
var food: T[];
}
struct Carrot
{
func eat(): void {}
}
struct Car {}
func main(): void
{
var carrotBox: FoodBox< Carrot >; // OK
carrotBox.food ~= Carrot(); // Adds a carrot
// var carBox: FoodBox< Car >; // Not allowed
static assert( not trait(compiles, func() { var carBox: FoodBox< Car >; } ));
}