type Rb = object of RootObj Sb = object of RootObj Rt = object of Sb method m1(this : var Rb, tx:ref Sb): void method m2(this : var Rt, rx: var Rb) : void = rx.m1( this.Sb )
The compiler complains of a type mismatch: got (Rb, Sb) but expected one of: m1(this: var Rb, tx: ref Sb)
I don't understand why when rx is a var in m2 that it does not recognise that it is var in the call to m1.
I don't understand how to give m1 a ref to its own base class.
Thanks for your insights.
First, if you want to use the object-oriented facilities, please use ref object instead of object. Object types cannot (safely) be used polymorphically.
Second, you cannot construct a ref to something that's not allocated on the heap (well, you can through casts, but it'd probably segfault). A ref pointer is a GC-ed reference and you cannot make it point to something that may be on the stack or allocated in static memory.
The following should work:
type
Rb = ref object of RootObj
Sb = ref object of RootObj
Rt = ref object of Sb
method m1(this : Rb, tx: Sb) = discard
method m2(this : Rt, rx: Rb) = rx.m1( this )
that come from C++ backgrounds.
Not only C++. My feeling is, that for all other languages with strong focus on OO, like Python, Ruby, Java, there is some work to translate the existing code to Nim. (I have a still unfinished schematics editor written in Ruby and still no good ideas how to convert it to Nim...).
We may read the Goran Krampe Blog about OOP, maybe that will help, see http://goran.krampe.se/2014/10/29/nim-and-oo/