0

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?

2

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)
2
  • The class Node as per the need in question should expect a parameter of type Node 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) – user116948 Jan 31 '14 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. – Paul Hicks Jan 31 '14 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.