Recursive discriminated unions [Phobos2]

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Feb 15 10:44:14 PST 2009


Chris Nicholson-Sauls wrote:
> I've noticed a shortcoming that I somehow hadn't come across before. The 
> same issue exists in the current Phobos2, so it isn't anything new. 
>  Apparently (and this may be known to all but me already) in order to 
> use an array type, you must also use its element type.  Doing otherwise 
> results in tripping a static assert.
> 
> Example:
> 
>     module test2;
> 
>     import std.variant;
> 
>     alias Algebraic!(void, string) var_t;
> 
>     void main () {
>         var_t foo = "quux";
>     }

This was an oversight caused by implementing opIndex, which I now fixed 
with credit. Thanks!

Now if you haven an Algebraic containing an array, you won't be able to 
call its opIndex directly; it will throw an exception. This is because 
opIndex wants to return the same type as this, and that type is unable 
to hold the element type of the array!

[snip]
> Specifically the statement (*result = (*me)[index];) triggers it, by 
> spawning an opAssign!(elem_type).  I'm not sure how to cleanly alter it 
> right off the top of my head.
> 
> ----------
> 
> On the other hand, the new form This[] works fine, so far.  The only 
> missing feature is an opApply().

Ok, I filed a bug report on your behalf.

> The form This[This]... Using 'foo.get!(table_t)[k] = v;' results in a 
> range violation.  For the moment, they are essentially immutable hashes. 
>  Could actually be fine for a number of purposes.

That's because foo.get returns by value. Try using peek, which returns a 
pointer. Could you please post some code if that doesn't work for your case?

> ----------
> 
> If I get some extra free time this weekend (assuming V-day isn't the 
> death of me) I'll hack away at it some and see if I can't figure some 
> things out, on all counts above.
> 
> Oh, and THANKS.  :)

Advantage #1 of marrying a foreigner: you get to convince her that V-day 
is an artificial event created for commercial interests only because 
they want to sell blood diamonds. Watching "Blood Diamond" together a 
week before V-day recommended.


Andrei



More information about the Digitalmars-d mailing list