www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - cast(typeid.. ?

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
Is it possible that this will be possible in D2.0?

I was trying to improve Andy Friesens makeArrays template
and i had a really great solution in mind:

foreach (int index, TypeInfo ti; _arguments)
{
       T j = cast(T)(*cast(typeid(ti)*)(_argptr));
       _argptr += _arguments[index].sizeof;
       result[index]=j;
}

Only problem: the "cast(typeid(ti))" part isn't
possible :(

The obvious solution would be to expand the tests
to test for every basic type, but this would be much
simpler and understandable?
Jul 18 2004
next sibling parent Derek <derek psyc.ward> writes:
On Sun, 18 Jul 2004 11:39:43 +0200, Ivan Senji wrote:

 Is it possible that this will be possible in D2.0?
 
 I was trying to improve Andy Friesens makeArrays template
 and i had a really great solution in mind:
 
 foreach (int index, TypeInfo ti; _arguments)
 {
        T j = cast(T)(*cast(typeid(ti)*)(_argptr));
        _argptr += _arguments[index].sizeof;
        result[index]=j;
 }
 
 Only problem: the "cast(typeid(ti))" part isn't
 possible :(
 
 The obvious solution would be to expand the tests
 to test for every basic type, but this would be much
 simpler and understandable?

Sure would. I guess the problem is that cast() is a compile-time operation and typeid() is a run-time operation. -- Derek Melbourne, Australia
Jul 18 2004
prev sibling parent reply quetzal <quetzal_member pathlink.com> writes:
In article <cddgm0$270h$1 digitaldaemon.com>, Ivan Senji says...
Is it possible that this will be possible in D2.0?

I was trying to improve Andy Friesens makeArrays template
and i had a really great solution in mind:

foreach (int index, TypeInfo ti; _arguments)
{
       T j = cast(T)(*cast(typeid(ti)*)(_argptr));
       _argptr += _arguments[index].sizeof;
       result[index]=j;
}

Only problem: the "cast(typeid(ti))" part isn't
possible :(

Ofcourse it is not. "ti" is not a type after all.
The obvious solution would be to expand the tests
to test for every basic type, but this would be much
simpler and understandable?

You can try writing something like that: # foreach (int index, TypeInfo ti; _arguments) # { # T j = va_arg!(T)(_argptr); # _argptr += _arguments[index].sizeof; # result[index] = j; # } Since you already know type of array element why not using it? I assume you want to use your makeArray template like.. # int[] i_array; float[] f_array; # makeArray!(int)(i_array, 1, 2, 3, 4, 5); # makeArray!(float)(f_array, 1.1, 2.2, 3.3, 4.4, 5.5);
Jul 18 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"quetzal" <quetzal_member pathlink.com> wrote in message
news:cddpl3$2a0b$1 digitaldaemon.com...
 In article <cddgm0$270h$1 digitaldaemon.com>, Ivan Senji says...
Is it possible that this will be possible in D2.0?

I was trying to improve Andy Friesens makeArrays template
and i had a really great solution in mind:

foreach (int index, TypeInfo ti; _arguments)
{
       T j = cast(T)(*cast(typeid(ti)*)(_argptr));
       _argptr += _arguments[index].sizeof;
       result[index]=j;
}

Only problem: the "cast(typeid(ti))" part isn't
possible :(

Ofcourse it is not. "ti" is not a type after all.

Ofcourse! But wouldn't it be nice if typeid(ti) was a type?
The obvious solution would be to expand the tests
to test for every basic type, but this would be much
simpler and understandable?

You can try writing something like that: # foreach (int index, TypeInfo ti; _arguments) # { # T j = va_arg!(T)(_argptr); # _argptr += _arguments[index].sizeof; # result[index] = j; # } Since you already know type of array element why not using it? I assume you want to use your makeArray template like.. # int[] i_array; float[] f_array; # makeArray!(int)(i_array, 1, 2, 3, 4, 5); # makeArray!(float)(f_array, 1.1, 2.2, 3.3, 4.4, 5.5);

And like this: real[] X = makeArray!(real)(1,300,1.32); As you can see, this has mixed types, and simple casting of void* _argptr to a pointer to real won't work. I thought it would be nice to first cast void* to the type it actually is, and then cast it to real. This way 1 would be cast to byte(or something like that) and then to real. I know this may be imposible but i'm just haivng crazy ideas :)
Jul 18 2004