www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variadic arguments and map

reply Frank Benoit <keinfarbton googlemail.com> writes:
How to access a map from a vararg list.

If the function does process the TypeInfo array and gets an argument
with TypeInfo_AssociativeArray, I don't see a way to access the map
content without writing code, that has knowledge of the internal map
storage. I need to understand how the runtime for AA is implemented, to
access the content.

I used this way ...
1. cast the vararg pointer to some kind of map, e.g. ubyte[ubyte]
2. foreach over the map with inout value
3. get the address of the value => p.
4. It is know, that the key follows the value: p - "used key size" get
the key address
5. Get the value address with adding the size of key from the TypeInfo

Is there a better way? (more portable and less pointer arithmethic)
Nov 20 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Frank Benoit" <keinfarbton googlemail.com> wrote in message 
news:fhvdep$1pe5$1 digitalmars.com...
 How to access a map from a vararg list.

 If the function does process the TypeInfo array and gets an argument
 with TypeInfo_AssociativeArray, I don't see a way to access the map
 content without writing code, that has knowledge of the internal map
 storage. I need to understand how the runtime for AA is implemented, to
 access the content.

 I used this way ...
 1. cast the vararg pointer to some kind of map, e.g. ubyte[ubyte]
 2. foreach over the map with inout value
 3. get the address of the value => p.
 4. It is know, that the key follows the value: p - "used key size" get
 the key address
 5. Get the value address with adding the size of key from the TypeInfo

 Is there a better way? (more portable and less pointer arithmethic)
This looks like how std.format does it too..
Nov 20 2007
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Jarrett Billingsley schrieb:

 This looks like how std.format does it too.. 
 
 
Is this a feature missing from the D language? Better access to variadic stuff.
Nov 20 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Frank Benoit" <keinfarbton googlemail.com> wrote in message 
news:fhvifa$221n$1 digitalmars.com...
 Jarrett Billingsley schrieb:

 This looks like how std.format does it too..
Is this a feature missing from the D language? Better access to variadic stuff.
Runtime variadics are really kind of hacky in just about any systems programming language. You'll notice that most scripting languages and VM'ed languages (Python, Ruby?, Java, .Net) pass instead an array of Object, since Object can hold anything. Runtime variadics are also slow and possibly unsafe, since you have to do all your typechecking and dispatch at runtime, and if you don't do it right, you could end up with a malfunctioning function (or at the very least, one that doesn't follow i.e. the same conversion rules as the language). A very nice alternative to runtime variadics is to use a variadic template function. This gives you a list of the argument types at compile time, giving you the opportunity to write much more efficient code, at the expense of code bloat, since you technically now have a different overload of the function for every different list of parameter types. Templated functions also can't be virtual. A nice alternative would be, once D2 gets opImplicitCastFrom (is that what it's called?) implemented, to have a Variant type and use a variadic array of those: void func(Variant[] args...) { foreach(arg; args) { // if it's an int, do something... } } // implicitly constructs a Variant struct out of each param func(3, "hi", new Object()); Then you get virtualizability, ease of calling, no code bloat, and even though you still have to determine types at runtime, it'll likely be a lot more straightforward as a variant type will probably have methods to check if it can be cast to certain types, etc.
Nov 20 2007