Thread overview
[Issue 13352] Algebraic does not support binary arithmetic when omitting small number types
Aug 22, 2014
Sönke Ludwig
Aug 22, 2014
Sönke Ludwig
August 22, 2014
https://issues.dlang.org/show_bug.cgi?id=13352

Sönke Ludwig <sludwig@outerproduct.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #1 from Sönke Ludwig <sludwig@outerproduct.org> ---
https://github.com/D-Programming-Language/phobos/pull/2452

--
August 22, 2014
https://issues.dlang.org/show_bug.cgi?id=13352

bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc

--- Comment #2 from bearophile_hugs@eml.cc ---
assert(a + b == 3L); // error
assert(a + 2 == 3L); // error
assert(1 + b == 3L); // error

I think giving an error is correct. An algebraic data type is not meant to be transparent to arithmetic operations, it's a way to bundle alternative types.

--
August 22, 2014
https://issues.dlang.org/show_bug.cgi?id=13352

--- Comment #3 from Sönke Ludwig <sludwig@outerproduct.org> ---
Well, "Algebraic!(uint, int, long)" would have worked. The intention of VariantN definitely is to make all operations transparently available, if you look at the source code. I also don't see a reason why that would be harmful. On the other hand, it can be extremely useful.

See for example the new std.data.json proposal, which is the reason for this bug report. It allows "JSONValue" to be used in a much more convenient way.

--
September 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13352

--- Comment #4 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/cb5d83dad9c22e973f494006e874771eddf833dc Fix issue #13352 - Only perform Algebraic arithmetic with allowed types.

Adds a static check allowed!T before trying any particular numeric type. Also checks for an exact type match in case of a raw (non-Variant) operand and prefers that to one of the fixed numeric types. Finally, it loosens the restriction for opArithmetic to only work for equal Variant types.

Conflicts:
    std/variant.d

https://github.com/D-Programming-Language/phobos/commit/67db0d68a78d48b0c2b3c2b6cbe9b8ee8ad855d6 Merge pull request #2452 from s-ludwig/issue_13352

Fix issue #13352 - Only perform Algebraic arithmetic with allowed types.

--