Internal error from DMD
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Mon Jun 26 05:31:34 PDT 2006
Bradley Smith wrote:
> Jarrett Billingsley wrote:
>
>> Sure. Just wondering what you're trying to store? There might be a
>> simpler way to store it, whatever it is; I've personally never had to
>> use boxes, though maybe you're coming from a dynamically typed
>> background.
>
>
> I'm experimenting with converting some Java code to D, and the code
> stores int, boolean, and String data in a list through the Integer and
> Boolean wrapper objects. Using the D boxer is the only way I've found to
> create a heterogeneous array which includes primitive data elements.
>
> Perhaps the need for boxing could be eliminated by making the code more
> D-like, but at this point, I'm only trying a straight translation.
>
> Bradley
Well, if it only ever works with those three types, you could go with a Var struct.
# enum VarType { Int, Bool, String }
#
# struct Var {
# VarType type ;
#
# union {
# bool b ;
# int i ;
# char[] s ;
# }
# }
#
# Var[] array;
You can simplify creation of Var structs with a set of static call operators, like such:
# struct Var {
# // ...
# static Var opCall (bool value) {
# Var var ;
#
# var.type = VarType.Bool ;
# var.b = value ;
#
# return var;
# }
# }
While it isn't /the/ most elegant solution possible, its tried and true, and pretty
effective for definite sets of types.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d-learn
mailing list