I'll use C as an example language to show what I mean.
struct parent
{
int x;
char y;
};
struct child
{
char y;
int x;
};
int foo(void * s, type obj_type)
{
// the casting is done using a "type" variable
obj_type obj = (obj_type) s;
return obj->x;
}
int main(int argc, char** argv)
{
type obj_type = struct parent *;
struct parent p;
p.x = 0;
//returns 0
foo(&p, obj_type);
obj_type = struct child *;
struct child c;
c.x = 5;
// returns 5
foo(&c, obj_type);
return 0;
}
As you can see, x is placed in different locations in memory for both structs so I can't just have a static offset in memory. Is this possible in C in anyway (some preprocessor magic I couldn't think of)? I assume no, but are there any languages where types can themselves be used as variables? I'd love to explore the implications of type-centered programming
EDIT: as itsme86 pointed out, C# has this ability with the Type class. Also C++ Concepts and Haskell Type class are of interest.