[Issue 5467] New: library-based typedef

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 20 08:50:31 PST 2011


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 at puremagic.com
        ReportedBy: mrmocool at gmx.de


--- Comment #0 from Trass3r <mrmocool at 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: -------


More information about the Digitalmars-d-bugs mailing list