https://forum.dlang.org/post/[email protected]https://dlang.org/library/std/typecons/typedef.html
--------------------------------
import std.typecons;
import std.stdio;
alias MyInt = Typedef!int;
void f(MyInt mi) {}
void main() {
MyInt a = 2;
MyInt b = 3;
f(a + b);
}
--------------------------------
td.d(14,4): Error: function td.f(Typedef!(int, 0, null) mi) is not callable using argument types (int)
td.d(14,4): cannot pass argument a.opBinary(b) of type int to parameter Typedef!(int, 0, null) mi
Naturally I've expected `a + b` will have the same type as MyInt (not the underlying type int).
Is this a typedef overlook, or it's a feature by design?
If it's by design what's the reason behind?
I think this is a bug, because on the doc page, the purpose of Typedef is:
Typedef allows the creation of a unique type which is based on an existing type. Unlike the alias feature, Typedef ensures the two types are not considered as equals.
https://dlang.org/library/std/typecons/typedef.html
Comment #1 by schveiguy — 2020-06-11T14:45:48Z
Typedef was meant to replace the deprecated typedef feature.
If you look at D1's spec https://digitalmars.com/d/1.0/type.html, it claims that if two identical typedefs are the parameters to a binary arithmetic operator, the result should be the same type.
This works today with enums as well. This should be how Typedef operates.
The solution may be tricky, because one has to define opBinary for the specific type, but must forward to the base type if there is no match.
Comment #2 by mingwu — 2023-10-18T22:31:26Z
Created attachment 1894
s7.i.gz attached. dmd s7.i shows the same error.
s7.i.gz attached.
dmd s7.i shows the same error.
Comment #3 by mingwu — 2023-10-18T22:36:49Z
Comment on attachment 1894
s7.i.gz attached. dmd s7.i shows the same error.
wrong operation, this is for issue #24187
Comment #4 by mingwu — 2023-10-30T05:17:20Z
Created attachment 1896
reduced to 2 lines: s2.i
Comment #5 by mingwu — 2023-10-30T05:19:53Z
wrong operation, this is for issue #24187
Comment #6 by robert.schadek — 2024-12-01T16:36:56Z