What does a cast really do?

Quirin Schroll qs.il.paperinik at gmail.com
Fri Feb 21 01:02:26 UTC 2025


It seems a cast does more than change the static type and VRP.
```d
void foo(uint) { }

int x = -1;
foo(x); // compiles (debatable)
foo(long(x)); // compiles(!)
foo(cast(long)x); // compiles(!)
foo((() => cast(long)x)()); // Error: foo is not callable using 
argument types […]
```

Why do the latter two work? Their static type is `long` which 
normally rules out conversion to `uint`. However, if VRP can 
prove the value is definitely in the range of `uint`, the 
implicit conversion to `uint` is possible. However, VRP shouldn’t 
say that that’s the case, since `int` supports negative numbers 
and `uint` doesn’t.


More information about the Digitalmars-d-learn mailing list