Bug 13352 – Algebraic does not support binary arithmetic when omitting small number types
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2014-08-22T09:26:00Z
Last change time
2014-09-11T08:53:45Z
Keywords
pull
Assigned to
nobody
Creator
sludwig
Comments
Comment #0 by sludwig — 2014-08-22T09:26:41Z
For example, when defining Algebraic!(long) and then trying to add two such
values, or an algebraic with a raw number, opArithmetic will try to instantiate
an Algebraic!(long) with an int, which fails compilation.
---
alias TP = Algebraic!(long);
auto a = TP(1L);
auto b = TP(2L);
assert(a + b == 3L); // error
assert(a + 2 == 3L); // error
assert(1 + b == 3L); // error
---
Comment #2 by bearophile_hugs — 2014-08-22T10:18:31Z
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.
Comment #3 by sludwig — 2014-08-22T10:39:36Z
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.
Comment #4 by github-bugzilla — 2014-09-11T08:53:45Z