www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error with implicit cast of ^^=

reply wjoe <invalid example.com> writes:
```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
```

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:

```D
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
```D
mixin("x = cast(typeof(x))(x" ~ op[0..$-1] ~ " y);");
```
but I'm curious if there's a less convoluted way.
Jul 13 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 13 2021
parent reply wjoe <invalid example.com> writes:
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!
Jul 14 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 14 2021
parent wjoe <invalid example.com> writes:
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.
Jul 15 2021