digitalmars.D.learn - Error with implicit cast of ^^=
- wjoe (28/28) Jul 13 2021 ```D
- =?UTF-8?Q?Ali_=c3=87ehreli?= (11/21) Jul 13 2021 If it's important, std.conv.to checks the value:
- wjoe (3/25) Jul 14 2021 I was planning on adding support for over-/underflow bits but
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/12) Jul 14 2021 If so, then there is also std.experimental.checkedint:
- wjoe (2/15) Jul 15 2021 Thanks I'll have a look.
```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
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
On Tuesday, 13 July 2021 at 15:14:26 UTC, Ali Çehreli wrote:On 7/13/21 4:12 AM, wjoe wrote:I was planning on adding support for over-/underflow bits but this is much better. Thanks!```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 14 2021
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
On Wednesday, 14 July 2021 at 17:29:04 UTC, Ali Çehreli wrote:On 7/14/21 2:44 AM, wjoe wrote:Thanks I'll have a look.x = (x ^^ y).to!(typeof(x)); } For example, run-time error if y == 7.I was planning on adding support for over-/underflow bits butthis ismuch 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 15 2021