Hi there !
I am trying to chain procedures that mutate a reference object, but I can't
managed to do it.
Here is a minimal example:
type
Child* = ref object
value: int
Parent* = ref object
child: Child
proc set_value*(self: var Child, value: int): Child {.discardable.} =
self.value = value
return self
proc get_child*(self: var Parent): Child {.discardable.} =
var child = Child()
self.child = child
return child
var parent = Parent()
parent.get_child().set_value(10)
Run
This example triggers the following compile error, but I dont understand why I
can't mutate the reference return by the get_child procedure.
Error: type mismatch: got <Child, int literal(10)>
but expected one of:
proc set_value(self: var Child; value: int): Child
first type mismatch at position: 1
required type for self: var Child
but expression 'get_child(parent)' is immutable, not 'var'
expression: set_value(get_child(parent), 10)
Run
Any ideas ?
Thanks :)