www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - library defined typedef

reply Trass3r <un known.com> writes:
We had a thread where a suggestion was made for a library defined typedef:

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 || 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 );
     }
}

One problem is it still lacks proper support for explicit and implicit  
casting.
But will it be included in typecons?
I certainly need a proper solution to this for my coding.
Jul 25 2010
next sibling parent Trass3r <un known.com> writes:
Probably it's no bad idea to repeat these explanations from Andrei so they  
don't get lost in the huge original thread:


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;
Jul 25 2010
prev sibling parent Trass3r <un known.com> writes:
Bump.

 We had a thread where a suggestion was made for a library defined  
 typedef:

 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 || 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 );
      }
 }

 One problem is it still lacks proper support for explicit and implicit  
 casting.
 But will it be included in typecons?
 I certainly need a proper solution to this for my coding.
Dec 02 2010