I wrote a Nim procedure taking two objects as var
parameters. They each are an object with an int
field level
. Before doing the real work of the procedure, I want to put the parameters in order by which has the larger level
, so I do this:
proc subroutine(param1: var MyObject, param2: var MyObject) =
var large: ptr MyObject = param1.addr
var small: ptr MyObject = param2.addr
# If in wrong order by level, swap large and small
if large.level < small.level:
large = param2.addr
small = param1.addr
# The rest of the proc references, only variables large and small, e.g.,
large.level += 1
small.level += 2
This seems to work for my application, but I notice that in the Nim documentation, the ptr
type is called "unsafe", and it is suggested to be used only for low-level operations. There's a "safe" reference type ref
, and it is suggested to use ref
unless you really want to do manual memory management.
I don't want to do manual memory management, and I want the Nim garbage collector to handle the memory for these parameters for me, but I don't see a way to get safe ref
's to the two parameters.
I really want to be able to write the algorithm (which is significantly more complex than the simple code I showed) in terms of the variables large
and small
, rather than param1
and param2
. Otherwise, if I can only reference parameters param1
and param2
, without making these aliases for them, I'd have to copy and paste the same algorithm twice to separately handle the cases param1.level < param2.level
and param1.level >= param2.level
.
Is there a more idiomatic Nim way to do something like this, without using a ptr
type?
ptr
? Or perhaps it's more idiomatic Nim not to usevar
parameters so freely, and instead use a more functional approach, returning new objects that represent modified versions of the parameters? – Rickart