Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 11, 2009 3 variant questions | ||||
---|---|---|---|---|
| ||||
How do I return a variant type in D1? After assessing that a variadic argument is an array, how do I check its depth? How do I set the variable given to me through the _arguments array? thanks :) |
May 11, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote: > How do I return a variant type in D1? > After assessing that a variadic argument is an array, how do I check its depth? > How do I set the variable given to me through the _arguments array? > thanks :) Variant func() { return variant(5); } If you want to return something given from a runtime variadic argument list, neither Tango's Variant nor Phobos's Variant will work. Tango's Variant will get this capability soon, I hear. There is also reflect.variant: svn co http://felt-project.org/reflect reflect.variant allows you to create a Variant from a void* and a TypeInfo, like you get with varargs. |
May 11, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | "Christopher Wright" <dhasenan@gmail.com> wrote in message news:gu8v1b$1uth$1@digitalmars.com... > Saaa wrote: >> How do I return a variant type in D1? >> After assessing that a variadic argument is an array, how do I check its >> depth? >> How do I set the variable given to me through the _arguments array? >> thanks :) > > Variant func() > { > return variant(5); > } I can't seem to find Variant in D1 > > If you want to return something given from a runtime variadic argument list, neither Tango's Variant nor Phobos's Variant will work. Tango's :( > Variant will get this capability soon, I hear. > > There is also reflect.variant: > svn co http://felt-project.org/reflect > > reflect.variant allows you to create a Variant from a void* and a TypeInfo, like you get with varargs. Shouldn't it be possible to set an variadic argument? void func( ... ) { //something like this cast(int) _argptr = 10; } |
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
> "Christopher Wright" <dhasenan@gmail.com> wrote in message news:gu8v1b$1uth$1@digitalmars.com...
>> Saaa wrote:
>>> How do I return a variant type in D1?
>>> After assessing that a variadic argument is an array, how do I check its depth?
>>> How do I set the variable given to me through the _arguments array?
>>> thanks :)
>> Variant func()
>> {
>> return variant(5);
>> }
> I can't seem to find Variant in D1
In D1/Phobos, you can use std.boxer.Box, which can work with variadic arguments.
|
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | > In D1/Phobos, you can use std.boxer.Box, which can work with variadic arguments.
Ah, I see. boxer saves the typeinfo along the data.
Sorry to ask, but do you have an answer to my other question as well? Passing variadic arguments as ref I think is what I am asking for :)
|
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa |
Saaa wrote:
> ...
> Passing variadic arguments as ref I think is what I am asking for :)
You can't. You have to explicitly take the address of the arguments.
-- Daniel
|
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep |
> Saaa wrote:
>> ...
>> Passing variadic arguments as ref I think is what I am asking for :)
>
> You can't. You have to explicitly take the address of the arguments.
>
> -- Daniel
Like this ?
*_argptr = 10;
:D
I don't know how to tell the compiler I want to write data there.
|
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa |
Saaa wrote:
>> Saaa wrote:
>>> ...
>>> Passing variadic arguments as ref I think is what I am asking for :)
>> You can't. You have to explicitly take the address of the arguments.
>>
>> -- Daniel
>
> Like this ?
> *_argptr = 10;
> :D
>
> I don't know how to tell the compiler I want to write data there.
import std.stdarg;
assert( _arguments[0] is typeid(int*) );
auto arg = va_arg!(int*)(_argptr);
*arg = 10;
Probably.
-- Daniel
|
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | I just noticed D1 does have std.stdarg. I shouldn't just search on the website :( (where it is missing on the phobos page) > import std.stdarg; > > assert( _arguments[0] is typeid(int*) ); > auto arg = va_arg!(int*)(_argptr); > *arg = 10; > > Probably. :D > > -- Daniel So, you make arg point to the same as va_arg. Why is this extra step necessary and why won't simple casting not work? |
May 12, 2009 Re: 3 variant questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote: > I just noticed D1 does have std.stdarg. > I shouldn't just search on the website :( > (where it is missing on the phobos page) > >> import std.stdarg; >> >> assert( _arguments[0] is typeid(int*) ); >> auto arg = va_arg!(int*)(_argptr); >> *arg = 10; >> >> Probably. > :D >> -- Daniel > > So, you make arg point to the same as va_arg. No, va_arg is a function. > Why is this extra step necessary and why won't simple casting not work? You should never directly work with _argptr. It's not guaranteed to be a simple pointer. For example, I believe that GCC will sometimes use registers (God only knows WHY). var_arg!(T) will convert _argptr into the type you specify and it will also advance _argptr to the next argument. -- Daniel |
Copyright © 1999-2021 by the D Language Foundation