[Issue 12597] Payload getter for std.typecons.Typedef

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Sep 25 21:24:56 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=12597

--- Comment #13 from monkeyworks12 at hotmail.com ---
(In reply to bearophile_hugs from comment #12)
> (In reply to monkeyworks12 from comment #11)
> > I think this enhancement request should be closed now that
> > https://github.com/D-Programming-Language/phobos/pull/2116 has been merged.
> > With that PR, your first example now becomes:
> > 
> > void main() {
> >     import std.typecons: Typedef;
> >     import std.math: sin;
> >     alias Angle = Typedef!double;
> >     Angle x = 0.5;
> >     auto y1 = sin(x); // Error.
> >     auto y2 = sin(cast(TypedefType!Angle)x); // OK.
> > }
> > 
> > A bit verbose, but it accomplishes what you want and is more DRY and safer
> > than cast(double)x.
> 
> This ER asks for a function like "typedefVal" that's usable like:
> 
> auto y2 = x.typedefVal.sin; // OK.
> 
> Casts are unsafe, their usage should be minimized in D code. So this ER is
> still valid.

But casting to TypedefType!(typeof(x)) is always safe, so if you want such a
function, it's trivial to add one yourself. The main problem (getting the
underlying type of a Typedef) is solved.

auto typedefVal(T)(T val)
{
    return cast(TypedefType!T)val;
}

--


More information about the Digitalmars-d-bugs mailing list