Concepts like c++20 with specialized overload resolution.

Steven Schveighoffer schveiguy at gmail.com
Sat May 27 16:38:43 UTC 2023


On 5/27/23 9:50 AM, vushu wrote:
> On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:
>> On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:
>>> [...]
>>> Is there something equivalent in dlang, doesn't seem like d support 
>>> specialized overload?
>>
>> D solution is called [template 
>> constraints](https://dlang.org/spec/template.html#template_constraints).
> 
> Yes I know there is template constraint, but not with specialized 
> overloading right?
> 
> so you need to use static if for checking if it hasmagma.

What is missing is an "else" thing.

So you have to repeat the constraint (as a negation) unfortunately.

e.g.:

```d
struct LavaMan {
   void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
   void try_making_lava() { writeln(" Making fake lava"); }
};

void make_lava(T)(ref T lava) if (hasMagma!T) {
     lava.magma();
}

void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
     lava_thing.try_making_lava();
}
```

-Steve


More information about the Digitalmars-d-learn mailing list