www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need help on working with TypeInfo, typeid(), and typeof()

reply David L. Davis <SpottedTiger yahoo.com> writes:
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
Aug 12 2005
next sibling parent "John C" <johnch_atms hotmail.com> writes:
"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 
Aug 12 2005
prev sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"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.
Aug 12 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
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, 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? 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
Aug 12 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"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, 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 :-)
Aug 12 2005
parent David L. Davis <SpottedTiger yahoo.com> writes:
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, 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
Aug 12 2005