need clarification: will typedef, C struct initialization, etc.
Simen kjaeraas
simen.kjaras at gmail.com
Wed Jun 2 13:50:58 PDT 2010
Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
>> struct Typedef( T ) {
>> T payload;
>> alias payload this;
>> }
>>
>> alias Typedef!int myInt;
>>
>> There you go.
>
> Yah, perfect - that would be the subtype. I think we should work on
> adding a pseudo-supertype as well, and also on a completely independent
> type. Then we can add these abstractions to std.typecons.
Actually, alias this makes it more of a parallel type.
This code, however, allows for subtypes, supertypes, parallel types, as
well as independent types. please critique.
enum Type {
Independent,
Super,
Sub,
Parallel,
}
struct Typedef( T, T init = T.init, Type type = Type.Sub, 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 );
}
}
unittest {
alias Typedef!( int, 1, Type.Super ) superInt;
alias Typedef!( int, 2, Type.Sub ) subInt;
alias Typedef!( int, 3, Type.Independent ) independentInt;
alias Typedef!( int, 3, Type.Parallel ) parallelInt;
int i;
superInt m1;
subInt m2;
independentInt m3;
parallelInt m4;
// Construct
static assert( is( typeof({ superInt j = i; })));
static assert( is( typeof({ subInt j = i; })));
static assert( !is( typeof({ IndependentInt j = i; })));
static assert( is( typeof({ parallelInt j = i; })));
// Assign from base
static assert( is( typeof({ m1 = i; })));
static assert( !is( typeof({ m2 = i; })));
static assert( !is( typeof({ m3 = i; })));
static assert( is( typeof({ m4 = i; })));
// Assign to base
static assert( !is( typeof({ i = m1; })));
static assert( is( typeof({ i = m2; })));
static assert( !is( typeof({ i = m3; })));
static assert( is( typeof({ i = m4; })));
// Assign from other type
static assert( is( typeof({ m1 = m2; })));
static assert( !is( typeof({ m2 = m1; })));
static assert( !is( typeof({ m1 = m3; })));
static assert( !is( typeof({ m2 = m3; })));
static assert( !is( typeof({ m3 = m1; })));
static assert( !is( typeof({ m3 = m2; })));
static assert( is( typeof({ m1 = m4; })));
static assert( !is( typeof({ m2 = m4; })));
static assert( !is( typeof({ m3 = m4; })));
static assert( !is( typeof({ m4 = m1; })));
static assert( is( typeof({ m4 = m2; })));
static assert( !is( typeof({ m4 = m3; })));
}
--
Simen
More information about the Digitalmars-d
mailing list