digitalmars.D.bugs - [Issue 5467] New: library-based typedef
- d-bugmail puremagic.com (46/46) Jan 20 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5467
- d-bugmail puremagic.com (50/50) Jan 20 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5467
- d-bugmail puremagic.com (10/10) Dec 30 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5467
- d-bugmail puremagic.com (19/27) Jan 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=5467
- d-bugmail puremagic.com (15/30) Jan 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=5467
http://d.puremagic.com/issues/show_bug.cgi?id=5467 Summary: library-based typedef Product: D Version: D1 & D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Phobos AssignedTo: nobody puremagic.com ReportedBy: mrmocool gmx.de --- Comment #0 from Trass3r <mrmocool gmx.de> 2011-01-20 08:48:32 PST --- 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; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 20 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5467 --- Comment #1 from Trass3r <mrmocool gmx.de> 2011-01-20 08:51:08 PST --- 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 ); } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 20 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5467 Jonathan M Davis <jmdavisProg gmx.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jmdavisProg gmx.com --- Comment #2 from Jonathan M Davis <jmdavisProg gmx.com> 2011-12-30 03:23:17 PST --- https://github.com/D-Programming-Language/phobos/pull/300 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 30 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5467 Stewart Gordon <smjg iname.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |smjg iname.com --- Comment #3 from Stewart Gordon <smjg iname.com> 2012-01-01 13:50:10 PST --- (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.... -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 01 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5467 Robert Clipsham <robert octarineparrot.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |robert octarineparrot.com --- Comment #4 from Robert Clipsham <robert octarineparrot.com> 2012-01-02 00:15:40 GMT --- (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> -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 01 2012