Thread overview
Save a type in a variable
Jan 25, 2007
Heinz
Jan 25, 2007
Pragma
Jan 25, 2007
Kirk McDonald
Jan 26, 2007
pragma
January 25, 2007
Hi, i have a class and i need to retrieve a type as a parameter in its constructor, example: (can be made easily in c#)

class myclass
{
        type mytype;

        this(type t)
        {
                 mytype = t;
        }
}

I don't have a single approach to do this. i need a clue pliss? an example woyld be great hehe, thx
January 25, 2007
Heinz wrote:
> Hi, i have a class and i need to retrieve a type as a parameter in its constructor, example: (can be made easily in c#)
> 
> class myclass
> {
>         type mytype;
>                 this(type t)
>         {
>                  mytype = t;
>         }
> }
> 
> I don't have a single approach to do this. i need a clue pliss? an example woyld be great hehe, thx

You want to use 'TypeInfo' at runtime (instead of 'type'), and use typeof(T) at compile time:

for example:

TypeInfo mytype = typeof(int);


-- 
- EricAnderton at yahoo
January 25, 2007
Pragma wrote:
> Heinz wrote:
> 
>> Hi, i have a class and i need to retrieve a type as a parameter in its constructor, example: (can be made easily in c#)
>>
>> class myclass
>> {
>>         type mytype;
>>                 this(type t)
>>         {
>>                  mytype = t;
>>         }
>> }
>>
>> I don't have a single approach to do this. i need a clue pliss? an example woyld be great hehe, thx
> 
> 
> You want to use 'TypeInfo' at runtime (instead of 'type'), and use typeof(T) at compile time:
> 
> for example:
> 
> TypeInfo mytype = typeof(int);
> 
> 

That should be:

TypeInfo mytype = typeid(int);

typeid() returns a type's TypeInfo at runtime. typeof() infers the type of an expression at compile-time.

Note that TypeInfo is just information about a type. (You can compare the TypeInfos of two items to see if they are the same type.) You cannot create variables of a type with just its TypeInfo. (That kind of trickery is what templates are for.)

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
January 26, 2007
Kirk McDonald wrote:
> Pragma wrote:
>> Heinz wrote:
>>
>>> Hi, i have a class and i need to retrieve a type as a parameter in its constructor, example: (can be made easily in c#)
>>>
>>> class myclass
>>> {
>>>         type mytype;
>>>                 this(type t)
>>>         {
>>>                  mytype = t;
>>>         }
>>> }
>>>
>>> I don't have a single approach to do this. i need a clue pliss? an example woyld be great hehe, thx
>>
>>
>> You want to use 'TypeInfo' at runtime (instead of 'type'), and use typeof(T) at compile time:
>>
>> for example:
>>
>> TypeInfo mytype = typeof(int);
>>
>>
> 
> That should be:
> 
> TypeInfo mytype = typeid(int);
> 
> typeid() returns a type's TypeInfo at runtime. typeof() infers the type of an expression at compile-time.
> 
> Note that TypeInfo is just information about a type. (You can compare the TypeInfos of two items to see if they are the same type.) You cannot create variables of a type with just its TypeInfo. (That kind of trickery is what templates are for.)
> 
ack... um, what he said. :)

sorry for the misinformation.