Bug 5467 – library-based typedef

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-01-20T08:48:32Z
Last change time
2020-03-21T03:56:35Z
Assigned to
No Owner
Creator
Trass3r

Comments

Comment #0 by hoganmeier — 2011-01-20T08:48:32Z
Typedef is scheduled for deprecation but there is no proper library-based solution to it yet. Andrei pointed out the following use cases: 1. Something that's just like another type yet "parallel" with it. This is good for abstractions that encode different units of measurement that aren't supposed to be mixed. ParallelTypedef!double Miles; Such a type should accept explicit initialization from a regular double: auto dist = Miles(3.2); However it shouldn't accept initialization from another parallel typedef: ParallelTypedef!double Kms; auto dist1 = Kms(4); auto dist2 = Miles(dist1); // no Arithmetic operations should only work within Miles but not mixing Miles with other types. Here's where things already get complicated - you do want to allow some operations between Miles and double (e.g. "*"), in some others you don't (e.g. "+"). Here's where a library facility would help: ParallelTypdef!(double, "allow_arithmetic", "allow_mixed:*,/,%") Miles; 2. Opaque "handle" types that can be used with overloading. The base type of the typedef is just the storage strategy: OpaqueTypedef!int FileHandle; Such a typedef supports no arithmetic and no implicit conversions. You can explicitly initialize it from an int and you can cast it back to it using an explicit cast. 3. Proper subtype. Create a true subtype of a type that allows explicit initialization from the type and implicit conversion to the type. SubtypeTypedef!Exception MyException; 4. Proper supertype. The base type implicitly converts to the introduced type, but not vice versa. SupertypeTypedef!uint Bits;
Comment #1 by hoganmeier — 2011-01-20T08:51:08Z
And that's the latest code draft I could find: enum Type { Independent, Super, Sub, Parallel, } struct Typedef( T, Type type = Type.Sub, T init = T.init, string _f = __FILE__, int _l = __LINE__ ) { T payload = init; static if ( type != Type.Independent ) { this( T value ) { payload = value; } } static if ( type == Type.Sub) { // typedef int foo; foo f; // f.opCast!(t)() == cast(t) f T opCast(T)() { return payload; } } static if ( type == Type.Sub || type == Type.Parallel ) { alias payload this; } static if ( type == Type.Super ) { typeof( this ) opAssign( T value ) { payload = value; return this; } } else static if ( type == Type.Sub ) { @disable void opAssign( T value ); } }
Comment #2 by issues.dlang — 2011-12-30T03:23:17Z
Comment #3 by smjg — 2012-01-01T13:50:10Z
(In reply to comment #0) > ParallelTypdef!(double, "allow_arithmetic", "allow_mixed:*,/,%") > Miles; This leaves much to be desired: we want to allow Miles * double but not Miles * Miles, and Miles + Miles but not Miles + double. Moreover, Miles / Miles wants to return double. Maybe we need a more specialised version of ParallelTypedef that does primitive units checking. opMul and opDiv would themselves be templates therein. Maybe I'll have a go at implementing something when I have a bit more time.... > 2. Opaque "handle" types that can be used with overloading. The base type of > the typedef is just the storage strategy: So it's basically a struct wrapper. > 3. Proper subtype. Create a true subtype of a type that allows explicit > initialization from the type and implicit conversion to the type. This seems the closest to how typedefs behave at the moment. > 4. Proper supertype. The base type implicitly converts to the introduced type, > but not vice versa. Makes sense, but I'm not sure what practical it would have....
Comment #4 by robert — 2012-01-01T16:15:40Z
(In reply to comment #0) > 1. Something that's just like another type yet "parallel" with it. This is good > for abstractions that encode different units of measurement that aren't > supposed to be mixed. > > ParallelTypedef!double Miles; > > Such a type should accept explicit initialization from a regular double: > > auto dist = Miles(3.2); > > However it shouldn't accept initialization from another parallel typedef: > > ParallelTypedef!double Kms; > auto dist1 = Kms(4); > auto dist2 = Miles(dist1); // no <bikeshed> This needs a better name, parallel is transitive, so Miles(dist1) should work (if there are 3 things, A, B and C, A is parallel to B and B is parallel to C, then A is parallel to C). </bikeshed>
Comment #5 by b2.temp — 2017-01-23T22:56:26Z
this should have been closed for a while.... (std.typecons.TypeDef)