Thread overview
Implicit cast primitive type construction
Oct 10
ryuukk_
Oct 11
ryuukk_
Oct 10
Daniel N
Oct 11
ryuukk_
October 10

I was doing this:

cstats.stamina = cstats.stamina + 1;

but it doesn't compile

you'd think this code compile, nope, you have to uglify your code with:

cstats.stamina = cstats.stamina + cast(ubyte) 1);

or this if you do other stuff

cstats.stamina = cast(ubyte) (cstats.stamina + 1);

this makes the code messier and annoying to type

what about the following then?

cstats.stamina = cstats.stamina + ubyte(1);

..same error message...

i don't know how any of this is called, if you know, please enlighten me

but it would be cool if it'd work

October 11
Integer promotion rules, these have been argued plenty, you'll want to look at the history on the N.G. for why they exist the way that they do.
October 10

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;

October 10

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:

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@79

October 10

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...

    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@79

October 11

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;

My mistake sorry, it's cdef.stamina + 1, so can't do += 1, i should have copy pasted the whole block instead for clarity

October 11

On Thursday, 10 October 2024 at 18:58:51 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Integer promotion rules, these have been argued plenty, you'll want to look at the history on the N.G. for why they exist the way that they do.

Now that i know how it's called i could check the documentation, thanks

Here the link for future readers: https://dlang.org/spec/type.html#integer-promotions

I think the error message could be improved to mention what it actually is then