Overview
I have been reading through several implementations of programming languages (mainly CPython and YARV, plus a couple smaller languages), and I have noticed two main different ways of implementing objects.
They both usually define some sort of enum representing the type of the object, for example
enum ObjectType {
OBJECT_INT,
OBJECT_FLOAT,
OBJECT_STRING
// loads more types
};
Then they usually go choose one of two different methods.
Method 1
Define structs for each different type of object, with a base object all these object can be cast to. For example:
// base object
struct Object {
enum ObjectType type;
};
struct ObjectInt {
struct Object object;
// other fields specific to this type of object
};
struct ObjectFloat {
struct Object object;
// other fields specific to this type of object
};
struct ObjectString {
struct Object object;
// other fields specific to this type of object
};
Method 2
Create one struct for all objects, with a union inside.
struct Object {
enum ObjectType type;
union {
struct {
// fields specific to this type of object
} as_int;
struct {
// fields specific to this type of object
} as_float;
struct {
// fields specific to this type of object
} as_string;
};
};
Summary
Both methods allow you to represent the same things, but method 1 involves a lot more casting, whereas method 2 involves a lot more dereferencing. Does one offer anything the other doesn't, in terms of speed, space or features, or are they just two implementations of the same thing?
Thanks in advance.
Edit
An example of method 1 in YARV can be seen here