Movement against float.init being nan

Bastiaan Veelo Bastiaan at Veelo.net
Sat Aug 20 21:07:33 UTC 2022


On Saturday, 20 August 2022 at 04:04:08 UTC, Ali Çehreli wrote:
> 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

Enter `std.typecons.Typefef`:

```d
import std;

alias Distance = Typedef!(double, 0.0, "distance");
alias Temperature = Typedef!(double, 0.0, "temperature");

void main()
{
     Temperature t;
     Distance d;
     d += 4.5;
     // t = d; // does not compile
}
```
However, type information can get lost in intermediate results:
```d
     t = 0.5 + d; // result is double
```

— Bastiaan


More information about the Digitalmars-d mailing list