| |
| Posted by vushu in reply to ryuukk_ | PermalinkReply |
|
vushu
Posted in reply to ryuukk_
| On Saturday, 27 May 2023 at 18:41:47 UTC, ryuukk_ wrote:
> On Saturday, 27 May 2023 at 17:49:27 UTC, vushu wrote:
> On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer wrote:
> On 5/27/23 9:50 AM, vushu wrote:
> On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:
> [...]
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.:
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
I see thanks for the example :), I think this probably the closest equivalent i dlang.
I feel like overload in that case make things harder to read
My example has less context switch, and hte logic is correctly understandable at first sight
Only one make_lava function
It depends this example is quite small and a static if is sufficient, if you have a lot of cases it would make sense to split thing up into overloaded functions.
imagine you are writing a library for handling vector or matrices that has a common
add function. I just want to know the equivalent thing in dlang vs c++.
|