vendredi 17 mai 2019

Creating types with restricted properties

There are occasions where the possible values of a type need to be restricted according to some properties. Example floats or math vectors are required to be normalized. Is it a good practice to create classes for these cases and use operator overload to switch between the types?

For example have a vector2 and vector2_normalized class where the operators of vector2_normalized that can change the length of the vector (+, -, scalar * and /, ..) return a vector2 instance and the others return a vector2_normalized instance. Then use implicit conversion to automatically change between the two. This way vectors which must be normalized can use this type and normalization errors are eliminated.

Aucun commentaire:

Enregistrer un commentaire