typedef alternative

Yao G. nospamyao at gmail.com
Sat Aug 14 19:04:56 PDT 2010


On Sat, 14 Aug 2010 20:34:02 -0500, Jonathan M Davis  
<jmdavisprog at gmail.com> wrote:

> Well, if you search the D list for typedef, you should find some posts  
> that
> suggest templated structs combined with alias which get you typedefs. It  
> sounds
> like something like that will probably end up in Phobos at some point,  
> though
> apparently there are some issues with conversions (though what they  
> would be if
> you're trying to keep the types completely separate, I don't know).
>
> - Jonathan M Davis

Indeed. I did a search on the news group and found this code in a Trass3r  
post:

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

I think I'll do something similar as this. I don't need either explicit or  
implicit cast between types.


-- 
Yao G.


More information about the Digitalmars-d-learn mailing list