Bug 11706 – Add a TypedefType trait to extract the underlying type of a std.typecons.Typedef

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-07T07:20:00Z
Last change time
2014-09-08T23:27:49Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-12-07T07:20:40Z
In dmd 2.065alpha this code doesn't work (see Issue 11704 ): void main() { import std.typecons: Typedef; import std.conv: to; alias T = Typedef!int; string x = "5"; T y = to!T(x); } A workaround is: T y = T(to!int(x)); A problem with this work-around is that "int" needs to be repeated, it's not DRY. So I suggest to add to Typedef an alias of the underlying type, so you can write: T y = T(to!(T.originalType)(x)); This alias is useful for other cases.
Comment #1 by bearophile_hugs — 2014-04-22T21:36:17Z
An alternative solution is to use something in std.traits to get the underlying type of a Typedef.
Comment #2 by andrej.mitrovich — 2014-04-24T10:39:37Z
(In reply to bearophile_hugs from comment #1) > An alternative solution is to use something in std.traits to get the > underlying type of a Typedef. That's a fantastic idea, and we can implement this thanks to recent compiler improvements. A pull is coming.
Comment #3 by andrej.mitrovich — 2014-04-24T10:45:17Z
Comment #4 by bearophile_hugs — 2014-04-24T11:08:31Z
(In reply to Andrej Mitrovic from comment #3) > https://github.com/D-Programming-Language/phobos/pull/2116 Thank you. But: > // safely cast to get the value that's being wrapped > int x = cast(OriginalType!MyInt)myInt; casts are not safe. That example is not good. Safer and shorter: int x = myInt.get;
Comment #5 by andrej.mitrovich — 2014-04-24T11:10:11Z
(In reply to bearophile_hugs from comment #4) > (In reply to Andrej Mitrovic from comment #3) > > https://github.com/D-Programming-Language/phobos/pull/2116 > > Thank you. But: > > > // safely cast to get the value that's being wrapped > > int x = cast(OriginalType!MyInt)myInt; > > casts are not safe. That example is not good. > > Safer and shorter: > > int x = myInt.get; That's not safer if 'myInt' is a UDT that happens to define get(). You may end up calling a function that returns something else entirely.
Comment #6 by bearophile_hugs — 2014-04-24T11:21:14Z
(In reply to Andrej Mitrovic from comment #5) > That's not safer if 'myInt' is a UDT that happens to define get(). You may > end up calling a function that returns something else entirely. Right, sorry, see my comments in Issue 12597
Comment #7 by github-bugzilla — 2014-09-08T23:27:48Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f9048c94ef834b683b1ce30f5722c9e0969cdf74 Fix Issue 11706 - Add a TypedefType trait to extract the underlying type of a std.typecons.Typedef. https://github.com/D-Programming-Language/phobos/commit/bf8d579d590bc89d9a7462cbc366fc97665e4a73 Merge pull request #2116 from AndrejMitrovic/Fix11706 Issue 11706 - Add a TypedefType trait to extract the underlying type of a std.typecons.Typedef