Movement against float.init being nan

Ali Çehreli acehreli at yahoo.com
Sat Aug 20 04:04:08 UTC 2022


On 8/19/22 06:42, Hipreme wrote:

 > that float and double are initialized to `nan`.

I think nan is the right choice. As Walter said, integrals are the 
outliers because there is not non equivalent for them.

 > This is really bad

Although we are all guilty, what is worse is using fundamental types 
directly. I think the following is a solution to nan being inappropriate 
e.g. for Distance:

struct initted(T, T initValue) {
   T value = initValue;
   alias value this;
}

alias Distance = initted!(double, 0);

void main() {
   Distance d;  // Not a nan ;)
   d += 1.5;
   d /= 2;
   assert(d == 0.75);
}

However, it is not type-safe enough because one can mix and match 
Distance with e.g. Temperature, which may not be correct for the program.

Ali



More information about the Digitalmars-d mailing list