Is D painted into a corner?

user1234 user1234 at 12.de
Mon Sep 1 13:37:11 UTC 2025


On Monday, 1 September 2025 at 12:20:02 UTC, Kapendev wrote:
> Insane way to do things:
>
> ```d
> struct Matrix(R, C, T, bool canFly, bool isCute) if 
> ((isNumberType!T || isMathType!T) && isGoodType!T && 
> isMagicType!T) {
>     static if (canFly) {
>         this(Blah blah) {
>             // Blah blah...
>         }
>     } else static if (isCute) {
>         this(Blah blah) {
>             // Blah blah...
>         }
>     } else {
>         // Blah blah...
>     }
>     // Just more template hell...
> }
> ```

This example is actually an issue very specific to constructors, 
which cannot be overloaded if templatized. Non-ctor functions can 
be both overloaded and templatized.

I think the D style to prevent that is to use "static factories", 
i.e thanks to a global function you prevent the problem of having 
both template constraints and static `ifs`.


More information about the Digitalmars-d-learn mailing list