digitalmars.D.learn - Need help on working with TypeInfo, typeid(), and typeof()
- David L. Davis (16/16) Aug 12 2005 Does anyone know how to take a TypeInfo (example below) and use it's sto...
- John C (6/25) Aug 12 2005 Is this what you want?
- Ben Hinkle (10/18) Aug 12 2005 It's not possible since a TypeInfo is a runtime representation of a type...
- David L. Davis (115/136) Aug 12 2005 Ben, I'd like to create a function more like this one,
- Ben Hinkle (26/70) Aug 12 2005 I know it would be great if it were possible but the compiler has only o...
- David L. Davis (7/80) Aug 12 2005 Thanks Ben! :)
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
"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
"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
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...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.htmlDoes 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
"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...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 :-)"David L. Davis" <SpottedTiger yahoo.com> wrote in message news:ddis60$14r7$1 digitaldaemon.com...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?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
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...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.htmlIn article <ddj1t0$18d4$1 digitaldaemon.com>, Ben Hinkle says...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 :-)"David L. Davis" <SpottedTiger yahoo.com> wrote in message news:ddis60$14r7$1 digitaldaemon.com...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?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