Typedef!int + Typedef!int => int? is this a typedef overlook, or it's a feature by design?
RazvanN
razvan.nitu1305 at gmail.com
Thu Jun 11 04:11:24 UTC 2020
On Wednesday, 10 June 2020 at 22:47:49 UTC, mw wrote:
> Hi,
>
> I'm trying to follow this doc on typedef:
>
> 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?
>
> Thanks.
I think it is an overlook and therefore a bug. The spec says that
the whole point of Typedef is to create a type that is different
from the typedefed one (otherwise you would end up with a
glorified alias). However the implementation simply mixins a
generic implementation of opBinary that uses the underlying type
[1]. The definition of opBinary has no idea that it should wrap
the result in another type [2].
[1]
https://github.com/dlang/phobos/blob/master/std/typecons.d#L7472
[2]
https://github.com/dlang/phobos/blob/master/std/typecons.d#L6836
More information about the Digitalmars-d
mailing list