(emulating) weak typing and some other things

seany seany at uni-bonn.de
Fri Dec 20 09:30:53 PST 2013


I am sorry, perhaps the question could be answered with already 
available information, but I did not manage to figure it 
completely

I realize that D is a strongly typed language. But in certain 
scenarios i may want to use a weak typing, especially when 
handling structs which are very close to each other in content, 
but not equal, it would be nice to use to redefine the type while 
preserving the name.

So for example consider
struct S_A { //many contents};
and
struct S_B { //all contents of S_A +
int SomeExtra};

//do something
S_A o1;
S_B o2;
//do some morething
if(someCondition)
{
    S_A temp;
    S_B o1;
    o1 = o2;

    //set other stuff of temp in o1
    //do the extra processing on the SomeExtra integer
}

//continue processing o1


For this very particular simple example it is possible to do the 
same thing without bothering about type conversion. However, in 
general - although i can not strictly prove it - weak typing 
could be used to avoid too much branching, while I could, for 
EACH variation fo the struct possibly (or not) write a if-else 
braching, I would prefer some easier approach.

One approach would be to somehow make the comipler to ignore the 
shadowing definition, where o1 is redefined as S_B, and another 
approach can be to dynamically allocate elements to a stack

so if(someCondition)
{
//dynamically allocate the element int SomeExtra
//to S_A o1,

And then the next question is how to resolve the conflict that o1 
WAS a S_A, now has an extra element ..

Perhaps tuples would be an approach, but what are the differences 
between such a dynamic struct and a tuple?

thank you for your patience


More information about the Digitalmars-d-learn mailing list