Implicit cast primitive type construction
Salih Dincer
salihdb at hotmail.com
Thu Oct 10 23:21:43 UTC 2024
On Thursday, 10 October 2024 at 20:25:31 UTC, Daniel N wrote:
> On Thursday, 10 October 2024 at 17:52:36 UTC, ryuukk_ wrote:
>> I was doing this:
>>
>> cstats.stamina = cstats.stamina + 1;`
>>
>>
>> but it doesn't compile
>>
>
> basically the only solution is
> cstats.stamina += 1;
Using a variant can also be a good solution:
```d
alias T = ubyte;/*
char;//*/
alias R = const char;
import std.stdio, std.variant;
void main()
{
T u; R v;
writefln("%d : %d", u.sizeof,
(true ? u : v).sizeof); // 1 : 4
ubyte x1 = 16;
x1 *= x1; // equal x1 = cast(typeof(x1))(x1 * x1);
typeid(x1).writeln(": ", x1); // ubyte: 0
Variant x2 = 16;
x2 = x2 * x2;
typeid(x2).writeln(": ", x2); //
std.variant.VariantN!32LU.VariantN: 256
}
```
Integer promotion is a known thing. There's nothing we can do
about it. It is not a coincidence that he was chosen especially
int. If you are going to work on very large numbers, use BigInt
and 100% string.
SDB at 79
More information about the dip.ideas
mailing list