how to create an IntSet on the heap in nim? -


there various libraries in nim return actual objects, rather references. want object on heap (regardless of efficiency) -- instance when have generic procedure expects ref object.

the way construct intset on heap have found is:

proc newintset() : ref intset =   new(result)   assign(result[], initintset()) 

this seems work, feels hack. worry if seems work. (are structures copied "assign" cleaned up?) there better way? there more generic way work other objects?

your code valid. resulting reference subject garbage collection other referefence.

if find doing often, can define following makeref template rid of code repetition:

template makeref(initexp: typed): expr =   var heapvalue = new(type(initexp))   heapvalue[] = initexp   heapvalue 

here example usage:

import typetraits  type foo = object   str: string  proc createfoo(s: string): foo =   result.str = s  let x = makeref createfoo("bar") let y = makeref foo(str: "baz")  echo "x: ", x.type.name, " x.str = ", x.str 

which output:

x: ref foo x.str = bar 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -