RosettaCodeData/Task/Constrained-genericity/D/constrained-genericity-1.d
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

19 lines
320 B
D

enum IsEdible(T) = is(typeof(T.eat));
struct FoodBox(T) if (IsEdible!T) {
T[] food;
alias food this;
}
struct Carrot {
void eat() {}
}
static struct Car {}
void main() {
FoodBox!Carrot carrotsBox; // OK
carrotsBox ~= Carrot(); // Adds a carrot
//FoodBox!Car carsBox; // Not allowed
}