Thread overview
Error with implicit cast of ^^=
Jul 13
wjoe
Jul 14
wjoe
Jul 15
wjoe
July 13
byte x = some_val;
long y = some_val;

x ^^= y; // Error:  cannot implicitly convert expression pow(cast(long)cast(int)x, y) of type long to byte

Is there a way to do this via ^^= ?

This is part of a unittest for opIndexOpAssign where the type of x is that of i.opIndex(_i). It's generated from a list of operators like this:

part_int_t!(1, 8, 10) i;
static assert(is(typeof(i[0]): short));
static assert(is(typeof(i[1]): byte));
static assert(is(typeof(i[2]): bool));
static foreach(op; ["+=", "-=", "^^=", ...])
{{
  typeof(i[1]) x;
  mixin("x" ~ op ~ "y;");
  mixin("i[1]" ~ op ~ "y;");
  assert(i == x);
}}

I rewrote it to something like

mixin("x = cast(typeof(x))(x" ~ op[0..$-1] ~ " y);");

but I'm curious if there's a less convoluted way.

July 13
On 7/13/21 4:12 AM, wjoe wrote:
> ```D
> byte x = some_val;
> long y = some_val;
>
> x ^^= y; // Error:  cannot implicitly convert expression
> pow(cast(long)cast(int)x, y) of type long to byte

[...]

> I rewrote it to something like
> ```D
> mixin("x = cast(typeof(x))(x" ~ op[0..$-1] ~ " y);");
> ```
> but I'm curious if there's a less convoluted way.

If it's important, std.conv.to checks the value:

import std.conv;

void main() {
  byte x = 2;
  long y = 6;
  x = (x ^^ y).to!(typeof(x));
}

For example, run-time error if y == 7.

Ali

July 14
On Tuesday, 13 July 2021 at 15:14:26 UTC, Ali Çehreli wrote:
> On 7/13/21 4:12 AM, wjoe wrote:
> > ```D
> > byte x = some_val;
> > long y = some_val;
> >
> > x ^^= y; // Error:  cannot implicitly convert expression
> > pow(cast(long)cast(int)x, y) of type long to byte
>
> [...]
>
> > I rewrote it to something like
> > ```D
> > mixin("x = cast(typeof(x))(x" ~ op[0..$-1] ~ " y);");
> > ```
> > but I'm curious if there's a less convoluted way.
>
> If it's important, std.conv.to checks the value:
>
> import std.conv;
>
> void main() {
>   byte x = 2;
>   long y = 6;
>   x = (x ^^ y).to!(typeof(x));
> }
>
> For example, run-time error if y == 7.
>
> Ali

I was planning on adding support for over-/underflow bits but this is much better. Thanks!
July 14
On 7/14/21 2:44 AM, wjoe wrote:

>>   x = (x ^^ y).to!(typeof(x));
>> }
>>
>> For example, run-time error if y == 7.

> I was planning on adding support for over-/underflow bits but this is
> much better. Thanks!

If so, then there is also std.experimental.checkedint:

  https://dlang.org/phobos/std_experimental_checkedint.html

Andrei Alexandrescu introduced it in this presentation:

  https://dconf.org/2017/talks/alexandrescu.html

Ali

July 15
On Wednesday, 14 July 2021 at 17:29:04 UTC, Ali Çehreli wrote:
> On 7/14/21 2:44 AM, wjoe wrote:
>
> >>   x = (x ^^ y).to!(typeof(x));
> >> }
> >>
> >> For example, run-time error if y == 7.
>
> > I was planning on adding support for over-/underflow bits but
> this is
> > much better. Thanks!
>
> If so, then there is also std.experimental.checkedint:
>
>   https://dlang.org/phobos/std_experimental_checkedint.html
>
> Andrei Alexandrescu introduced it in this presentation:
>
>   https://dconf.org/2017/talks/alexandrescu.html
>
> Ali

Thanks I'll have a look.