• Forum
  • Lounge
  • how best to implement a generic variable

 
how best to implement a generic variable?

I'm currently toying with developing a scripting language, not to be used extensively, but as a learning experience. I'm currently stuck trying to decide how best to create a generic variable class.

Should I create:
A) a safe class that is limited in it's types and has a small footprint on the memory

or

B) An unsafe type that is unlimited in how it's implemented and has a relatively large footprint.

What do you guys think? I can do either easily, but it will effect how I make functions and objects work later.
In your language, what is the user capable of? Can they only use built-in types, or are they allowed to define their own types? How are these types created? Through a class mechanism or through a system similar to typedef?
Last edited on
class mechanism. the generic variable i'm talking about is more like the var keyword in some scripting languages. That can change data type based on the data given to it. for instance

var foo = "string"

echo foo

>> "string"

foo = 1

echo foo + 2

>> 3
I've decided to go with the safer method. A class with a nested struct populated with 1 string, 1 char 1, integer, 1 float, 1 bool...etc.
void pointers might be your friend here
1
2
3
4
5
6
7
8
9
10
11
12
void* GeneticVariable(int type)
{
    void* v = 0;
    switch(type)
    {
        case type_float:
            v = (void*)new float;
            break;
         //...
    }
    return v;
}


you could do it with a struct that holds the pointer, and the type. Just remember to convert it to the right type when youre going to use it, and to delete itdelete (float*)v; when youre finished
Last edited on
I've never been comfortable with void pointers :O though that would take a much smaller amount of memory :\ I'll think about it..it's better Idea than my method :O just not as safe :P
just not as safe :P
That depends on your code lol, void pointers are perfectly safe if you use them correctly

Anyway, just pick the easiest, you probably wont have to worry about memory usage too much with a scripting language youre making for fun
Just get it to work first, then you can worry about memory usage and speed :P
Last edited on
I've never been comfortable with void pointers


Hence not as safe.

No route is particularly difficult. as for memory usage? That's one of the things i'm trying to teach my self with this. optimizing my code for best performance. :D
I would go with the void pointer route; although, you don't need all that casting.

Something like this, maybe:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef enum {
	char_t = 0,
	unsigned_char_t,
	signed_short_t,
	unsigned_short_t,
	signed_int_t,
	unsigned_int_t,
	signed_long_t,
	unsigned_long_t,
	float_t,
	double_t,
	string_t
} type_t;

class var {
	private:
		type_t data_type;
		void* data;
	public:
		/* ... */
};

?

The only issue then is with knowing how large data has to be.
Is the static type supposed to allow user types? If not, you can just use a string for everything. This how simple weakly-typed languages do it. Otherwise, you need to implement a metatype system. There's no need to resort to unsafe approaches.
Does using strings work for floating point numbers? I know you can convert integers to strings and vice versa, but how does that work for floats? You need some way of accessing real numbers...

Edit: He implicitly said he needs user-defined types:
NGen wrote:
Can they only use built-in types, or are they allowed to define their own types? How are these types created? Through a class mechanism or through a system similar to typedef?
Seraphimsan wrote:
class mechanism
Last edited on
I would consider a boost::variant.

void pointers are never your friend.
Well, since you're writing the interpreter, can't you just take into account all defined classes and define your generic variable as a union of all of them? You could generate the overloaded operators needed to get assigning the variables possible, that is, assuming that your scripting language allows overloaded operators.
Last edited on
Overloading? heh I haven't gotten there yet. Incidentally I can't actually access my code at the moment, everything I own computer wise (minus my netbook) is packed away, I move into the dorms tomorrow. But I digress, the string idea should work well.
Topic archived. No new replies allowed.