Is there a way to instantiate a ref object like that ?
type
Node[K,V] = object
key:K
value:V
left, right: ref Node[K,V]
var tree = ref Node[int, string](key: 1, value: "hello")
I know I can do
type
Tree[K,V] = ref Node[K,V]
var tree = Tree[int, string](key: 1, value: "hello")
but it seems a bit odd to me since I don't really need that Tree type otherwise. One could also argue it's not perfectly consistent since I can already use ref Node[K,V] as the type of the fields. Being able to do ref Nodeint, string would be very convenient and natural in my opinion.
That is not currently possible, but has been an often-requested feature. That said, I'm not sure I see the rationale behind not making Node[K,V] an object instead of a ref object type, since you can't represent the empty tree this way (at least without making it a ref again, which would defeat the whole design).
For what it's worth, a common idiom is to have both types. E.g.:
type
NodeObj[K,V] = object
key:K
value:V
left, right: Node[K,V]
Node[K, V] = ref NodeObj[K, V]