Thread overview
[Issue 2074] New: Variant arithmetic operations fail
May 06, 2008
d-bugmail
May 06, 2008
d-bugmail
May 06, 2008
d-bugmail
May 06, 2008
d-bugmail
May 06, 2008
d-bugmail
May 06, 2008
d-bugmail
May 17, 2008
d-bugmail
May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074

           Summary: Variant arithmetic operations fail
           Product: D
           Version: 2.014
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Phobos
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: arkangath@gmail.com


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


-- 

May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074


andrei@metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|bugzilla@digitalmars.com    |andrei@metalanguage.com




------- Comment #1 from andrei@metalanguage.com  2008-05-06 10:54 -------
(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);
    }


-- 

May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074





------- Comment #2 from arkangath@gmail.com  2008-05-06 12:23 -------
I think that there may be other operations failing, opDiv being one of them. I'm unsure about opMod however.


-- 

May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074


andrei@metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED




------- Comment #3 from andrei@metalanguage.com  2008-05-06 12:25 -------
(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


-- 

May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074





------- Comment #4 from andrei@metalanguage.com  2008-05-06 18:02 -------
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.


-- 

May 06, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074





------- Comment #5 from arkangath@gmail.com  2008-05-06 18:33 -------
(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.


-- 

May 17, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2074


andrei@metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED




------- Comment #6 from andrei@metalanguage.com  2008-05-17 11:23 -------
Fixed in 2.014.


--