I can't remember but recently in one of the APIs I saw something like this class A (member:A){}
. Scala does support it and there is no problem creating such a class but how do we initiate it and what purpose would such a class serve?
1 Answer
Something like var childNode = new Node(parentNode)
. You'd use it whenever you want a reference to anything of the same type. In this case, it might be reasonable to allow a null-valued parameter:
var root = new Node(null)
var child = new Node(root)
-
The class
Node
as per the need in question should expect a parameter of typeNode
or it's subclass, I don't think we can pass an instance of parent class to it. I tried it in REPL and it doesn't work.>>class C {def cfunc = println("cfunc")} >>class B (member:B) extends C {def bfunc = println("bfunc")} >>val c = new C()
val b = new B(c) >><console>:10: error: type mismatch; found : C required: B val b = new B(c)
Jan 31, 2014 at 0:07 -
There's no reference to parent/child classes in the question or in my answer. In my answer, the objects have a parent/child relationship, not the classes. Jan 31, 2014 at 0:57