View mode: basic / threaded / horizontal-split · Log in · Help
August 12, 2005
Need help on working with TypeInfo, typeid(), and typeof()
Does anyone know how to take a TypeInfo (example below) and use it's stored
typeid(int) to define a local variable?

TypeInfo ti = typeid(int);

// 'v' becomes define as another 'TypeInfo',
// but how could I use the 'int' that's
// stored in 'ti' instead?
typeof(ti) v; 

It would sure be nice if there was a '.type' property to grab the TypeInfo's
underlining typeid() for defining a local variable...maybe there's a way and I
just haven't found it yet.

Thanks in advance for any help.

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
August 12, 2005
Re: Need help on working with TypeInfo, typeid(), and typeof()
"David L. Davis" <SpottedTiger@yahoo.com> wrote in message 
news:ddis60$14r7$1@digitaldaemon.com...
>
> Does anyone know how to take a TypeInfo (example below) and use it's 
> stored
> typeid(int) to define a local variable?
>
> TypeInfo ti = typeid(int);
>
> // 'v' becomes define as another 'TypeInfo',
> // but how could I use the 'int' that's
> // stored in 'ti' instead?
> typeof(ti) v;
>
> It would sure be nice if there was a '.type' property to grab the 
> TypeInfo's
> underlining typeid() for defining a local variable...maybe there's a way 
> and I
> just haven't found it yet.

Is this what you want?

   int x = 20;
   TypeInfo ti = typeid(typeof(x));

John.

>
> Thanks in advance for any help.
>
> David L.
>
> -------------------------------------------------------------------
> "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
> -------------------------------------------------------------------
>
> MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
August 12, 2005
Re: Need help on working with TypeInfo, typeid(), and typeof()
"David L. Davis" <SpottedTiger@yahoo.com> wrote in message 
news:ddis60$14r7$1@digitaldaemon.com...
>
> Does anyone know how to take a TypeInfo (example below) and use it's 
> stored
> typeid(int) to define a local variable?
>
> TypeInfo ti = typeid(int);
>
> // 'v' becomes define as another 'TypeInfo',
> // but how could I use the 'int' that's
> // stored in 'ti' instead?
> typeof(ti) v;

It's not possible since a TypeInfo is a runtime representation of a type and 
the local variable declaration requires the type at compile-time. If you 
want to carry a type around as a symbol you can use templates or aliases. 
For example you can do
template Foo(type) {
   type v;
}
to abstract out "int" from your code.
August 12, 2005
Re: Need help on working with TypeInfo, typeid(), and typeof()
In article <ddj1t0$18d4$1@digitaldaemon.com>, Ben Hinkle says...
>
>
>"David L. Davis" <SpottedTiger@yahoo.com> wrote in message 
>news:ddis60$14r7$1@digitaldaemon.com...
>>
>> Does anyone know how to take a TypeInfo (example below) and use it's 
>> stored
>> typeid(int) to define a local variable?
>>
>> TypeInfo ti = typeid(int);
>>
>> // 'v' becomes define as another 'TypeInfo',
>> // but how could I use the 'int' that's
>> // stored in 'ti' instead?
>> typeof(ti) v;
>
>It's not possible since a TypeInfo is a runtime representation of a type and 
>the local variable declaration requires the type at compile-time. If you 
>want to carry a type around as a symbol you can use templates or aliases. 
>For example you can do
> template Foo(type) {
>    type v;
> }
>to abstract out "int" from your code. 
>
>

Ben, I'd like to create a function more like this one,

# /+
# // This function would replace the other function below:
# void displayArgValues(...)
# {
#     if ( _arguments.length == 0)
#         return;
#
#     for (int i = 0; i < _arguments.length; i++)
#         writefln(" Argument[%d] Type=%8s, value=", i, _arguments[i], 
#                   va_arg!(typeof(_arguments[i])(_argptr)));   	
# }
# +/

instead of the larger version below...is it still possible to do this with a
template? And if not, couldn't Walter add a '.type' property to grab the
TypeInfo's underlining typeid() for use in defining a local variable at compile
time?

# //typeinfo.d
# private import std.stdio;
# private import std.stdarg;
# 
# void displayArguments(...)
# {
#     if ( _arguments.length == 0)
#         return;
#         
#     for (int i = 0; i < _arguments.length; i++)
#     {   
#         writef(" Argument[%d] Type=%8s, value=", 
#                  i, _arguments[i]);
#               
#         if (_arguments[i] is typeid(real))
#             writef(va_arg!(real)(_argptr));
#         else if (_arguments[i] is typeid(char)) 
#             writef(va_arg!(char)(_argptr));   
#         else if (_arguments[i] is typeid(wchar)) 
#             writef(va_arg!(wchar)(_argptr));   
#         else if (_arguments[i] is typeid(dchar)) 
#             writef(va_arg!(dchar)(_argptr));   
#         else if (_arguments[i] is typeid(byte))  
#             writef(va_arg!(byte)(_argptr));
#         else if (_arguments[i] is typeid(ubyte)) 
#             writef(va_arg!(ubyte)(_argptr));
#         else if (_arguments[i] is typeid(short)) 
#             writef(va_arg!(short)(_argptr));
#         else if (_arguments[i] is typeid(ushort)) 
#             writef(va_arg!(ushort)(_argptr));
#         else if (_arguments[i] is typeid(int))  
#             writef(va_arg!(int)(_argptr)); 
#         else if (_arguments[i] is typeid(uint)) 
#             writef(va_arg!(uint)(_argptr)); 
#         else if (_arguments[i] is typeid(long))  
#             writef(va_arg!(long)(_argptr));  
#         else if (_arguments[i] is typeid(ulong)) 
#             writef(va_arg!(ulong)(_argptr));
#         else if (_arguments[i] is typeid(float)) 
#             writef(va_arg!(float)(_argptr));
#         else if (_arguments[i] is typeid(double))
#             writef(va_arg!(double)(_argptr));
#         else if (_arguments[i] is typeid(ifloat)) 
#             writef(va_arg!(ifloat)(_argptr));
#         else if (_arguments[i] is typeid(idouble))
#             writef(va_arg!(idouble)(_argptr));
#         else if (_arguments[i] is typeid(cfloat)) 
#             writef(va_arg!(cfloat)(_argptr));
#         else if (_arguments[i] is typeid(cdouble))
#             writef(va_arg!(cdouble)(_argptr));
#         else if (_arguments[i] is typeid(ireal))
#             writef(va_arg!(ireal)(_argptr));
#         else if (_arguments[i] is typeid(creal))
#             writef(va_arg!(creal)(_argptr));
#         else if (_arguments[i] is typeid(bit))
#             writef(va_arg!(bit)(_argptr));
#         else if (_arguments[i] is typeid(Object))
#             writef(va_arg!(Object)(_argptr).toString());
#         else if (_arguments[i] is typeid(char[]))
#             writef(va_arg!(char[])(_argptr));
#         else if (_arguments[i] is typeid(wchar[]))
#             writef(va_arg!(wchar[])(_argptr));
#         else if (_arguments[i] is typeid(dchar[]))
#             writef(va_arg!(dchar[])(_argptr));
#         else if (_arguments[i] is typeid(void[]))
#             writef(va_arg!(void[])(_argptr));
#         else
#             return;
# 
#         writefln(); 
#     }  
# }
# 
# int main()
# {
#     displayArguments(123, 456U, 'C', "ABC"w, 45.78L, 123e+10+34i);
#     return 0;
# }

Output:
--------
C:\dmd>dmd typeinfo.d
C:\dmd\bin\..\..\dm\bin\link.exe typeinfo,,,user32+kernel32/noi;

C:\dmd>typeinfo
Argument[0] Type=     int, value=123
Argument[1] Type=    uint, value=456
Argument[2] Type=    char, value=C
Argument[3] Type= wchar[], value=ABC
Argument[4] Type=    real, value=45.78
Argument[5] Type= cdouble, value=1.23e+12+34i

C:\dmd>

Thanks again for any help in advance,

David L.

PS. John C., thanks for your comments.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
August 12, 2005
Re: Need help on working with TypeInfo, typeid(), and typeof()
"David L. Davis" <SpottedTiger@yahoo.com> wrote in message 
news:ddj37m$19pb$1@digitaldaemon.com...
> In article <ddj1t0$18d4$1@digitaldaemon.com>, Ben Hinkle says...
>>
>>
>>"David L. Davis" <SpottedTiger@yahoo.com> wrote in message
>>news:ddis60$14r7$1@digitaldaemon.com...
>>>
>>> Does anyone know how to take a TypeInfo (example below) and use it's
>>> stored
>>> typeid(int) to define a local variable?
>>>
>>> TypeInfo ti = typeid(int);
>>>
>>> // 'v' becomes define as another 'TypeInfo',
>>> // but how could I use the 'int' that's
>>> // stored in 'ti' instead?
>>> typeof(ti) v;
>>
>>It's not possible since a TypeInfo is a runtime representation of a type 
>>and
>>the local variable declaration requires the type at compile-time. If you
>>want to carry a type around as a symbol you can use templates or aliases.
>>For example you can do
>> template Foo(type) {
>>    type v;
>> }
>>to abstract out "int" from your code.
>>
>>
>
> Ben, I'd like to create a function more like this one,
>
> # /+
> # // This function would replace the other function below:
> # void displayArgValues(...)
> # {
> #     if ( _arguments.length == 0)
> #         return;
> #
> #     for (int i = 0; i < _arguments.length; i++)
> #         writefln(" Argument[%d] Type=%8s, value=", i, _arguments[i],
> #                   va_arg!(typeof(_arguments[i])(_argptr)));
> # }
> # +/
>
> instead of the larger version below...is it still possible to do this with 
> a
> template?

I know it would be great if it were possible but the compiler has only one 
shot at compiling the displayArgValues function and it has no clue what the 
different types will be all the call-sites at runtime. Sometimes it gets 
called with int and sometimes with double but there is only one 
displayArgValues. I would recommend using the new writefx function in 
std.stdio that accepts arguments and argptr and avoid the explicit casting 
since writef ends up using TypeInfos anyway. So I'd do something like
void displayArguments(...)
{
    if ( _arguments.length == 0)
        return;
    void* argptr = _argptr;
    for (int i = 0; i < _arguments.length; i++)
   {
        writef(" Argument[%d] Type=%8s, value=",
                 i, _arguments[i]);
        writefx(stdout, _arguments[i .. i+1], argptr);
        argptr = argptr + ((_arguments[i].tsize() + int.sizeof - 1) & 
~(int.sizeof - 1));
    }
    writefln();
 }

The magic argptr updating should be added to something like std.stdarg but 
for now just copy-paste it everywhere :-)
August 12, 2005
Re: Need help on working with TypeInfo, typeid(), and typeof()
In article <ddj43q$1avg$1@digitaldaemon.com>, Ben Hinkle says...
>
>
>"David L. Davis" <SpottedTiger@yahoo.com> wrote in message 
>news:ddj37m$19pb$1@digitaldaemon.com...
>> In article <ddj1t0$18d4$1@digitaldaemon.com>, Ben Hinkle says...
>>>
>>>
>>>"David L. Davis" <SpottedTiger@yahoo.com> wrote in message
>>>news:ddis60$14r7$1@digitaldaemon.com...
>>>>
>>>> Does anyone know how to take a TypeInfo (example below) and use it's
>>>> stored
>>>> typeid(int) to define a local variable?
>>>>
>>>> TypeInfo ti = typeid(int);
>>>>
>>>> // 'v' becomes define as another 'TypeInfo',
>>>> // but how could I use the 'int' that's
>>>> // stored in 'ti' instead?
>>>> typeof(ti) v;
>>>
>>>It's not possible since a TypeInfo is a runtime representation of a type 
>>>and
>>>the local variable declaration requires the type at compile-time. If you
>>>want to carry a type around as a symbol you can use templates or aliases.
>>>For example you can do
>>> template Foo(type) {
>>>    type v;
>>> }
>>>to abstract out "int" from your code.
>>>
>>>
>>
>> Ben, I'd like to create a function more like this one,
>>
>> # /+
>> # // This function would replace the other function below:
>> # void displayArgValues(...)
>> # {
>> #     if ( _arguments.length == 0)
>> #         return;
>> #
>> #     for (int i = 0; i < _arguments.length; i++)
>> #         writefln(" Argument[%d] Type=%8s, value=", i, _arguments[i],
>> #                   va_arg!(typeof(_arguments[i])(_argptr)));
>> # }
>> # +/
>>
>> instead of the larger version below...is it still possible to do this with 
>> a
>> template?
>
>I know it would be great if it were possible but the compiler has only one 
>shot at compiling the displayArgValues function and it has no clue what the 
>different types will be all the call-sites at runtime. Sometimes it gets 
>called with int and sometimes with double but there is only one 
>displayArgValues. I would recommend using the new writefx function in 
>std.stdio that accepts arguments and argptr and avoid the explicit casting 
>since writef ends up using TypeInfos anyway. So I'd do something like
> void displayArguments(...)
> {
>     if ( _arguments.length == 0)
>         return;
>     void* argptr = _argptr;
>     for (int i = 0; i < _arguments.length; i++)
>    {
>         writef(" Argument[%d] Type=%8s, value=",
>                  i, _arguments[i]);
>         writefx(stdout, _arguments[i .. i+1], argptr);
>         argptr = argptr + ((_arguments[i].tsize() + int.sizeof - 1) & 
>~(int.sizeof - 1));
>     }
>     writefln();
>  }
>
>The magic argptr updating should be added to something like std.stdarg but 
>for now just copy-paste it everywhere :-) 
>

Thanks Ben! :)

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Top | Discussion index | About this forum | D home