www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5467] New: library-based typedef

reply d-bugmail puremagic.com writes:
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



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
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5467




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
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5467


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com



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
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5467


Stewart Gordon <smjg iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com




 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
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5467


Robert Clipsham <robert octarineparrot.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |robert octarineparrot.com



00:15:40 GMT ---

 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