I've designed a context object that has a `udata` field. This field can store a reference to a user object, which is specified when the user creates the object. However, I'm not sure what type this field should be. I've tried ref object and ref RootObj , but both result in compilation errors.
import std/tables
type
CTX = object
udata: ref RootObj
A = ref object
B = ref object
var ctx = CTX()
ctx.udata = A()
ctx.udata = B()
ctx.udata = newTable[int,int]()
https://play.nim-lang.org/#pasty=FIxvAJtl
A workaround is to use pointer , and then handle GC_ref and GC_unref in the hook.
After a quick test, RootRef is incompatible with generic ref object but compatible with ref object of RootObj. maybe I can force users to inherit from RootObj
import std/tables
type
CTX = object
udata: ref RootObj
A = ref object
B = ref object
C = ref object of RootObj
var ctx = CTX()
#ctx.udata = A()
#ctx.udata = B()
#ctx.udata = newTable[int,int]()
echo (A is RootRef) # false
echo (C is RootRef) # true https://play.nim-lang.org/#pasty=hEMFfZrfmaybe I can force users to inherit from RootObj
Yes. Your API, your rules.