www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Casting ulong to int with nogc

reply Selim Ozel <sozel wpi.edu> writes:
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
next sibling parent Paul Backus <snarwin gmail.com> writes:
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
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
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
parent Selim Ozel <sozel wpi.edu> writes:
Great answers, thanks :)
Jul 29 2021
prev sibling parent reply Rumbu <rumbu rumbu.ro> writes:
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,
 Selim
Length 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
parent Selim Ozel <sozel wpi.edu> writes:
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:
 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
Length 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; } ```
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. S
Aug 16 2021