RosettaCodeData/Task/Constrained-genericity/D/constrained-genericity-1.d
2023-07-01 13:44:08 -04: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
}