The following program fails:
-----------------------
import std.stdio;
import std.variant;
Variant a;
Variant b;
void main ()
{
a=2;
b=3;
writeln(b-a);
}
-------------------------------
With the error:
testar.d(11): Error: overloads VariantN!(maxSize)(VariantN!(maxSize)
rhs) and VariantN!(maxSize)(VariantN!(maxSize)
lhs) both match argument list for opSub
Comment #1 by andrei — 2008-05-06T10:54:00Z
(In reply to comment #0)
> The following program fails:
>
> -----------------------
>
> import std.stdio;
> import std.variant;
>
> Variant a;
> Variant b;
>
> void main ()
> {
> a=2;
> b=3;
> writeln(b-a);
> }
>
> -------------------------------
>
> With the error:
>
> testar.d(11): Error: overloads VariantN!(maxSize)(VariantN!(maxSize)
> rhs) and VariantN!(maxSize)(VariantN!(maxSize)
> lhs) both match argument list for opSub
Thanks for the reports! The problem is surprisingly subtle: opSub and opSub_r are both templates, so both match for a-b. I fixed the bug by embarrassing manual duplication and will commit to next release unless somebody comes with a better solution.
In the meantime, you may want to replace in your_dmd_installation/src/phobos/src/variant.d the function opSub_r with the following hecatomb:
VariantN opSub_r(int lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(uint lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(long lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(ulong lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(float lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(double lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
VariantN opSub_r(real lhs)
{
return VariantN(lhs).opArithmetic!(VariantN, "-")(*this);
}
Comment #2 by arkangath — 2008-05-06T12:23:15Z
I think that there may be other operations failing, opDiv being one of them. I'm unsure about opMod however.
Comment #3 by andrei — 2008-05-06T12:25:19Z
(In reply to comment #2)
> I think that there may be other operations failing, opDiv being one of them.
> I'm unsure about opMod however.
Probably everything that has an opXyz_r, sigh.
Andrei
Comment #4 by andrei — 2008-05-06T18:02:22Z
I looked into it some more and I think the best engineering solution is to remove support for right-hand-side operations in Variant.
This would require the occasional explicitness, e.g. Variant(5) - x instead of 5 - x, but I see that as a small disadvantage that avoids a hecatomb of bloating in the source.
Please advise.
Comment #5 by arkangath — 2008-05-06T18:33:10Z
(In reply to comment #4)
> I looked into it some more and I think the best engineering solution is to
> remove support for right-hand-side operations in Variant.
>
> This would require the occasional explicitness, e.g. Variant(5) - x instead of
> 5 - x, but I see that as a small disadvantage that avoids a hecatomb of
> bloating in the source.
>
> Please advise.
>
Truly, it would be best to sacrifice the right-hand-side operations. The rationale for this decision however should be stated on the documentation. Perhaps someday a compromise of D's operator overloading (or template instantiation) may be reached which makes these operations possible.
Before closing the variant file however, have a look at #2073.