Casting ulong to int with @nogc

Rumbu rumbu at rumbu.ro
Fri Jul 30 05:20:37 UTC 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 at 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;
}
```





More information about the Digitalmars-d mailing list