www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixed aggregates

reply Nigel Sandever <nigelsandever btconnect.com> writes:
I am trying to get to grips with D and one of the first things I found myself 
wanting is a command line processing module. Usual thing; scans the input args 
and produces a data structure that contains them. 

Using an associative array to contain the parsed args seems logical. The rest
of 
the program can refer to them by name. The problem is how to construct an AA 
that hold a mix of data types? Strings; ints; floats; ranges etc.

I came up with 
	
	void *[ char[] ] config;

but that gets very messy when trying to use the data. Requiring (sometimes 
nonintuative) casting to access to the data.

Anyone got a better way?

Thanks. njs.
Sep 22 2004
next sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
Nigel Sandever wrote:

 I am trying to get to grips with D and one of the first things I found
 myself wanting is a command line processing module. Usual thing; scans the
 input args and produces a data structure that contains them.
 
 Using an associative array to contain the parsed args seems logical. The
 rest of the program can refer to them by name. The problem is how to
 construct an AA that hold a mix of data types? Strings; ints; floats;
 ranges etc.
 
 I came up with
 
 void *[ char[] ] config;
 
 but that gets very messy when trying to use the data. Requiring (sometimes
 nonintuative) casting to access to the data.
 
 Anyone got a better way?
 
 Thanks. njs.
You might want to try using one of the Variant data types that people have written (though it would probably also involve cast-like syntax). I don't have any links off the top of my head but searching the ng should turn some up. Another option is to somehow use structures instead of an associative array. The set of options for a program won't change during run-time so a structure could hold all the options. The cmd line processing code will need info about the struct but at least that is done in one place and not every time an option is accessed. An AA that contains mixed data will always require some casting at lookup time. -Ben
Sep 22 2004
prev sibling parent Deja Augustine <deja scratch-ware.net> writes:
You can always just use a struct & union combo:

struct CmdLineArg
{
  int datatype; // this stores what type of data it is
  union data    // this stores the actual data in a compacted format
  {
   float     f;
   int       i;
   char[255] s;
   ...
  }
}

CmdLineArg[ char[] ] config;

Since you treat different types of data differently, it's probably not 
that much of an impact to check the datatype before accessing the data 
and it would allow you to catch type mismatches pretty easily:

if(config["TheKey"].datatype == FloatType)
   blah = config["TheKey"].data.f;
else
   error("Floating point number expected");

-Deja

Nigel Sandever wrote:

 I am trying to get to grips with D and one of the first things I found myself 
 wanting is a command line processing module. Usual thing; scans the input args 
 and produces a data structure that contains them. 
 
 Using an associative array to contain the parsed args seems logical. The rest
of 
 the program can refer to them by name. The problem is how to construct an AA 
 that hold a mix of data types? Strings; ints; floats; ranges etc.
 
 I came up with 
 	
 	void *[ char[] ] config;
 
 but that gets very messy when trying to use the data. Requiring (sometimes 
 nonintuative) casting to access to the data.
 
 Anyone got a better way?
 
 Thanks. njs.
 
 
Sep 22 2004