Thread overview
Casting ulong to int with @nogc
Jul 28, 2021
Selim Ozel
Jul 28, 2021
Paul Backus
Jul 29, 2021
Kagamin
Jul 29, 2021
Selim Ozel
Jul 30, 2021
Rumbu
Aug 17, 2021
Selim Ozel
July 28, 2021

import std.conv:to;

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

July 28, 2021

On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:

>

import std.conv:to;

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.

July 29, 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.
July 29, 2021
Great answers, thanks :)
July 30, 2021

On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:

>

import std.conv:to;

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:

@nogc size_t
myFunction(size_t number) {
return number;
}
August 17, 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:

>

import std.conv:to;

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:

@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