digitalmars.D - Casting ulong to int with nogc
- Selim Ozel (14/14) Jul 28 2021 import std.conv:to;
- Paul Backus (8/20) Jul 28 2021 The reason `std.conv.to` doesn't work in `@nogc` is that it
- Kagamin (11/11) Jul 29 2021 I use this:
- Selim Ozel (1/1) Jul 29 2021 Great answers, thanks :)
- Rumbu (11/25) Jul 29 2021 Length property of arrays is architecture dependent returning
- Selim Ozel (5/34) Aug 16 2021 Thank you. I'll try this one as well. The conversion suggested
import std.conv:to; ```d import std.conv:to; nogc int myFunction(ulong number){ return to!int(number); } ``` Since std.conv.to is a non nogc myFunction throws a compiler error. From a practical perspective length property of arrays return ulong. What do you think is a reasonable way of casting that into int with no-gc. Thanks, Selim
Jul 28 2021
On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:import std.conv:to; ```d import std.conv:to; nogc int myFunction(ulong number){ return to!int(number); } ``` Since std.conv.to is a non nogc myFunction throws a compiler error. From a practical perspective length property of arrays return ulong. What do you think is a reasonable way of casting that into int with no-gc.The reason `std.conv.to` doesn't work in ` nogc` is that it throws an exception when the conversion fails, and exceptions require the GC. If you're ok with values larger than `int.max` causing overflow, you can just use `cast(int) number`. Otherwise, you will probably want to write your own conversion function that does overflow checking.
Jul 28 2021
I use this: int ToInt(in ulong u) pure { assert(u<=int.max); return cast(int)u; } int Count(E)(in E[] arr) pure { return ToInt(arr.length); } Well, it depends if you're fine with just an assert.
Jul 29 2021
On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:import std.conv:to; ```d import std.conv:to; nogc int myFunction(ulong number){ return to!int(number); } ``` Since std.conv.to is a non nogc myFunction throws a compiler error. From a practical perspective length property of arrays return ulong. What do you think is a reasonable way of casting that into int with no-gc. Thanks, SelimLength property of arrays is architecture dependent returning ulong for 64 bit and uint for 32 bit. You can use size_t as data type to cover both cases. Therefore your function can be portable without any conversion: ```d nogc size_t myFunction(size_t number) { return number; } ```
Jul 29 2021
On Friday, 30 July 2021 at 05:20:37 UTC, Rumbu wrote:On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:Thank you. I'll try this one as well. The conversion suggested above worked fine for my purposes but it does look a bit ugly :) I was not aware of the option to use soze_t. Simport std.conv:to; ```d import std.conv:to; nogc int myFunction(ulong number){ return to!int(number); } ``` Since std.conv.to is a non nogc myFunction throws a compiler error. From a practical perspective length property of arrays return ulong. What do you think is a reasonable way of casting that into int with no-gc. Thanks, SelimLength property of arrays is architecture dependent returning ulong for 64 bit and uint for 32 bit. You can use size_t as data type to cover both cases. Therefore your function can be portable without any conversion: ```d nogc size_t myFunction(size_t number) { return number; } ```
Aug 16 2021