On Friday, 15 August 2025 at 12:18:30 UTC, 0xEAB wrote:
It seems like you’ve accidentally used the wrong syntax for the
template parameter.
With the syntax from your code snippet you’ve put a constraint on
the types that can be substituted for `N` when instantiating the
template.
```d
N getCol(N : size_t)(N n) {
return n;
}
void main() {
writeln(getCol(0)); // works
writeln(getCol!uint(0)); // works
writeln(getCol(0f)); // fails
writeln(getCol!float(0)); // fails
}
```
This kind of constraint is commonly used with class-based
polymorphism where it allows you to restrict the type to a
certain class including subclasses.
```d
class Foo {}
class Bar {}
class Sub : Foo {}
auto doSth(T : Foo)(T value) {
return value;
}
void main() {
doSth(new Foo());
doSth(new Sub());
doSth(new Bar()); // fails
}
```