D styled data format, Json failed

Christopher Wright dhasenan at gmail.com
Wed May 6 03:45:33 PDT 2009


Saaa wrote:
> My first stab at the get function.
> As you might see, I need help :D
> Thanks!
> 
> How do I make the function take a variadic argument and get its type?

void get(in char[][] file, in char[] identifier, ...)
{
	TypeInfo type = _arguments[0];
	void* var = _argptr;
	// etc
}

> void get(in char[][] file, in char[] indentifier, void* var)
> {
> 
> TypeInfo type = typeof(var);
> int row;
> 
> // Is it possible to check it is an array, no matter the depth?

If you use tango:

import tango.core.RuntimeTraits;
if (isArray(type)) {}

Otherwise, just copy the code from RuntimeTraits. The functions you 
would need are: realType, isStaticArray, isDynamicArray, isArray

> if ( !( type ==  typeid(int) || type == typeid(int[][])  ) ) // || etc
> {
>   throw new exception;
>   //which kind of exception should I throw?

Define your own exception type.

> //what is the prototype of a function which has a variadic return type?

"Variadic" means that the function can take any number of arguments.

Use Variant, from std.variant or tango.core.Variant. It's typesafe. You 
could also return a void*, but that will require allocation.

> var = parse(file, row, type, identifier);
> //can all types hold null?

If you use Variant, you can check isEmpty / hasValue or some such.



More information about the Digitalmars-d-learn mailing list