std.boxer and arrays

Brad Roberts braddr at puremagic.com
Wed Oct 17 01:10:04 PDT 2007


Aarti_pl wrote:
> Brad Roberts pisze:
>> Aarti_pl wrote:
>>> Bill Baxter pisze:
>>>> Marcin Kuszczak wrote:
>>>>> Walter Bright wrote:
>>>>>
>>>>>> Carlos Santander wrote:
>>>>>>> Maybe I missed something, but does the inclusion of std.variant mean
>>>>>>> that std.boxer will be deprecated or removed at some point?
>>>>>>
>>>>>> Yes. If you're using boxer, take a serious look at variant, and 
>>>>>> see if
>>>>>> that works for you.
>>>>>
>>>>> There is missing functionality in variant though. I mean converting 
>>>>> variadic
>>>>> function parameters into array of Boxes. It's nice, so it should be
>>>>> retained IMHO.
>>>>
>>>> I just used std.boxer for the first time recently.
>>>> The thing that I found missing was methods for working with and 
>>>> converting boxed arrays (*not* arrays of boxes).  You can only 
>>>> unconvert to the exact type of the original, and there's no 
>>>> overloaded opIndex or getElem/setElem functions.  No way to ask what 
>>>> the type of an element is either.  I wrote some templates that did 
>>>> the trick for what I wanted, but I had to copy a few private things 
>>>> out of std.boxer to do it.  I'd file an enhancement request and 
>>>> maybe even a patch if I believed anyone would do anything about it.
>>>>
>>>> --bb
>>>>
>>>>
>>>
>>> You are lucky then :-)
>>> It seems that std.variant from D 2.0 has such a functionality...
>>>
>>> ----
>>>
>>> With boxing of variadic parameters I meant something like this:
>>>
>>> # void func(...) {
>>> #   Box[] barr = boxArray(_arguments, _argptr);
>>> # }
>>>
>>> It can be usefull e.g. for repassing arguments to other function.
>>>
>>> BR
>>> Marcin Kuszczak
>>
>> Take a closer look.  Std/variant.d contains:
>>
>> Variant[] variantArray(T...)(T args)
>> {
>>     Variant[] result;
>>     foreach (arg; args)
>>     {
>>         result ~= Variant(arg);
>>     }
>>     return result;
>> }
>>
>> It's a template rather than using the variadic options, but the end 
>> result is the same.
> 
> 
> But how to use it with non-template variadic functions?
> Could you please provide example?
> 
> ----
> And I if you could also address my second question (Variant inside 
> Variant):
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.announce&article_id=10305 
> 
> 
> Thanks in advance!
> 
> BR
> Marcin Kuszczak

You know.. the compiler and libraries are easily downloadable for you to 
play and test yourself...

In that example, b is declared as a Variant si it's a Variant.  After 
the assignment, b contains a copy of a since Variant is a typedef to a 
templated struct.

The usage for variantArray is shown in the examples and I've included 
one here for you as well.

$ cat /tmp/foo.d
import std.stdio;
import std.variant;

void main()
{
     Variant a;
     a=5;

     Variant b;
     b=a;

     writeln("a = ", a);
     writeln("b = ", b);

     Variant[] c;
     c = variantArray(1, 2, 3.1, 4);
     writeln("c = ", c);
}

$ /tmp/foo
a = 5
b = 5
c = [1 2 3.1 4]

Later,
Brad



More information about the Digitalmars-d mailing list