Creating Structs/Classes at runtime?

Brian Brady brian.brady1982 at gmail.com
Sun Feb 17 16:44:49 PST 2013


Hi

Ok first ... is this possible? Or wise?
Being mostly self taught with regards to programming I sometimes 
wonder if the way I'm going about something is even the right 
approach, never mind whether my code is logical. So if this seems 
ludicrous then please tell me my approach is silly and point me 
in the right direction ... :)

Ok so ...

I was reading in some data from a csv and wanted to read it into 
a struct called.
My problem is
1) my full csv is 4000 variables wide, and I really don't want to 
declare each variable in the struct (I've created a smaller one 
to test)
2) what if I wanted to read in different csvs of different sizes, 
I don't want to have to change my program each time?

After much googling, I thought I found something on RosettaCode 
that would do what I wanted 
(http://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime#D)
but when I try to use it, it doesn't work. (Its actually adding 
to a class, which may be better than a struct in this instance, 
but regardless, it doesn't work)

As my programming skills are mainly obtained through finding 
other work that does something similar to what I desire and 
stealing/hacking it, please be gentle if there is some glaringly 
foolish mistake that is obvious. :S

Thanks in advance
Brian

My code is:
module main;

import std.stdio;
import std.csv;
import std.string;
import std.file;
import std.array;
import std.variant;

class dataVector(T)
{
     private T[string] vars;
     @property T opDispatch(string key)() pure nothrow
     {
         return vars[key];
     }

     @property void opDispatch(string key, U)(U value)/*pure*/ 
nothrow
     {
         vars[key] = value;
     }
};

void fillVector(int size, string filenameToUse)
{
     writeln(size);
     uint loopNum = size;
     do
     {
         writeln(loopNum);
     //    auto test = dataVector!Variant();
/*
         auto file = File(filenameToUse, "r");
         foreach(line;file.byLine())
         {
             foreach(ob;csvReader!Data(text))
             {
                 dataVector.size ~= ob;
             }
         }*/
// ****************************************
// according to the RosettaCode this is how I should be able
// to create a variable a in the class test with a value of 
loopNum + 1
// ****************************************
         test.a = loopNum + 1;

  //       writeln(test.a);
         loopNum--;
     }
     while(loopNum != 0);
}

void main()
{
     string input;

     string filename = "/A/Path/To/The/Dark/Side/output.csv";
     auto file = File(filename, "r");

/* this is super dynamic, but I've managed to create a different 
function to calculate this, which I have omitted to keep the code 
concise */
     int numOfVars = 11;
     if (numOfVars > 0)
     fillVector(numOfVars, filename);
}

The csv I would eventually like to read in is of the form:

test1,303,-140,-166,-317,-310,414,-157,-360,-120,-89
test10,-1,70,-57,-101,112,137,-134,9,195,86
test100,367,78,-417,123,220,-234,-170,236,-218,-351
test1000,309,-178,-674,-202,514,218,-165,76,-82,-328
test10000,-131,142,6,-143,80,46,29,48,-84,-113


More information about the Digitalmars-d-learn mailing list