Hello there great community i am currently learning nim, and got stuck with this one, please kindly help..
The main idea is when passing parent as none to the newA proc, we will use the module var: root as the parent but somehow if i am passing parent as none(A) to the newA proc, it will return incorrect len, but it will display correct result if passing as some(root)
Thank you friends
i have a main.nim:
import options
import a
let child = newA(none(A), some("child a")) # <-- using this line i will get incorrect result
# let child = newA(some(root), some("child a")) <-- if using this line i will get the correct len
discard newA(some(child), some("child b"))
echo "@root:", root.children.len # expecting 1
echo "@child a:", child.children.len # expecting 1
and in a.nim
import options
type A* = ref object
name: Option[string]
parent: Option[A]
children*: seq[A]
var root* = A(
name: some("root"),
parent: none(A),
children: @[]
)
proc add_child*(self: A, child: A) =
self.children.add(child)
child.parent = some(self)
proc newA*(parent: Option[A], name: Option[string] = none(string)): A =
var new_child = A(
name: name,
parent: parent,
children: @[]
)
if parent.isNone():
echo "adding to root"
root.add_child(new_child)
else:
echo "adding to child"
parent.get().add_child(new_child)
return new_child It seems this is a compiler bug. I compiled it with devel Nim with -d:checkAbi option and got following error:
Nim/lib/nimbase.h:273:35: error: static assertion failed: "backend & Nim disagree on size for: A:ObjectType
Related issues: https://github.com/nim-lang/Nim/issues/16754 https://github.com/nim-lang/Nim/issues/21558