Implicit cast primitive type construction

Salih Dincer salihdb at hotmail.com
Thu Oct 10 23:50:42 UTC 2024


On Thursday, 10 October 2024 at 23:21:43 UTC, Salih Dincer wrote:
> 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:

I think I acted quickly before I could show exactly what I wanted 
to show. Now this code is talking...


```d
     alias T = ubyte;
     Variant x2 = T(16);
     x2.type.write(": ", x2);
     writeln(" (Variant)"); // ubyte: 16 (Variant)
     
     x2 = x2 * x2;
     x2.type.write(": ", x2);
     writeln(" (Variant)"); // uint: 256 (Variant)

     auto x3 = x2.get!int;
     typeid(x3).writeln(": ", x3); // int: 256

     x3 = T.max - x3;
     typeid(x3).writeln(": ", x3); // int: -1
```

SDB at 79




More information about the dip.ideas mailing list