Typing in the handmade hero source

Shouldn't you really make the Z, X and Y coords different TYPES? Seems to me you make a lot of typos with those coordinates. If Typing structs keeps you from making errors, then why not be consistent?

You make a LOT more of that kind of typos than you do mixing up actual TYPE definitions. And, after all, there is nothing even remotely similar between an X and an Z coordinate. They are not even in the same dimention ffs.

They would definitely be different types!!

I feel this would help you all a lot :D
What do you mean different types? char, float and double? How that will work? Afaik all types need to be floats.
Cant you create a xcoord type, ycoord type and so on, to let the compiler find those typos? Yes you can. That's the point of types, to avoid assignment of incompatible types. Doing this will prevent any x,y,z typo, from making it into the exe. Unless I misunderstand something.
Not sure I understand what you are suggesting. Something like this?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
struct XCoord { float Value; };
struct YCoord { float Value; };

struct v2
{
  XCorrd X;
  YCoord Y;
};

...

v2 A, B;

A.X.Value += B.X.Value;
A.Y.Value += B.Y.Value;


I don't see how this helps, because you still will need to type X or Y somewhere in code where you use individual coordinates. Yeah you can not do "A.X = A.Y" anymore, but I don't think it is needed to prevent that.

Edited by Mārtiņš Možeiko on
no, that wouldn't help, since you are actually adding the .Value members (that are all floats). mixing up X and Y there wouldn't produce an error.

in reference to your example code, i think he means being able to do

1
2
A.X += B.X;
A.Y += B.Y;


with operator overloading.

then you would get a type mismatch if you did something like

1
A.X += B.Y;

Edited by d7samurai on
Oh, you want to overload every single operator/function also? In this case operator +=. I don't think that is a feasible solution. Sure for Vector operations +/-/.. you could do that. But what if you need to add X to Y? Or Y to X in some cases? Then you need bunch of more operations. What if you need some function to operate with X and Y coordinate separately and Rectangle (or something) - bunch of more functions. This can easily explode exponentially if you need to support every single combination of different types.
no, not me. i am not even saying i agree. i just told you what i think HE meant.

ps. you don't need other functions / overloads to add X to Y or any other combination of coordinates. in those (rare) cases you just add their .Value members. i think his sole point was to protect against accidentally mixing up the X, Y and Zs by making them distinct types. which could be done - with just a little overloading.

Edited by d7samurai on