This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Parent/Child relationships

7 replies
Andrés Testi
Joined: 2009-01-22,
User offline. Last seen 42 years 45 weeks ago.

What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

private List children;

public void addChild(Child child){
child.setParent(this);
children.add(child);
}

}

Regards.

- Andrés

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: Parent/Child relationships
It may be my private opinion, but I have a deep feeling that storing a pointer to a parent in child record is pretty much destructive, architecture-wise.

2009/3/5 Andrés Testi <andres.a.testi@gmail.com>
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés



--
Thanks,
-Vlad
Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Parent/Child relationships
trait LOLParenting {
  type PARENT <: I_CAN_HAS_CHILDRE
  trait I_CAN_HAS_CHILDREN[T <: I_CAN_HAS_PARENT[_]] {
    private List<T> children;
    def addChild(t: T) = { ...}
    def removeChild(t: T) = { ...}
  }

  trait I_CAN_HAS_PARENT[T <: I_CAN_HAS_CHILDREN] {
    def setParent(t: T)
  }


class Parent extends I_CAN_HAS_CHILDREN[Child]

alex

On Thu, Mar 5, 2009 at 11:36 AM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés

Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Parent/Child relationships
Whoops! I was just playing with the idea and hit the send button.  For entertainment only.


On Thu, Mar 5, 2009 at 1:52 PM, Alex Boisvert <boisvert@intalio.com> wrote:
trait LOLParenting {
  type PARENT <: I_CAN_HAS_CHILDRE
  trait I_CAN_HAS_CHILDREN[T <: I_CAN_HAS_PARENT[_]] {
    private List<T> children;
    def addChild(t: T) = { ...}
    def removeChild(t: T) = { ...}
  }

  trait I_CAN_HAS_PARENT[T <: I_CAN_HAS_CHILDREN] {
    def setParent(t: T)
  }


class Parent extends I_CAN_HAS_CHILDREN[Child]

alex

On Thu, Mar 5, 2009 at 11:36 AM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés


David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Parent/Child relationships


On Thu, Mar 5, 2009 at 1:54 PM, Alex Boisvert <boisvert@intalio.com> wrote:
Whoops! I was just playing with the idea and hit the send button.  For entertainment only.

I waz very amuzed. :-) 



On Thu, Mar 5, 2009 at 1:52 PM, Alex Boisvert <boisvert@intalio.com> wrote:
trait LOLParenting {
  type PARENT <: I_CAN_HAS_CHILDRE
  trait I_CAN_HAS_CHILDREN[T <: I_CAN_HAS_PARENT[_]] {
    private List<T> children;
    def addChild(t: T) = { ...}
    def removeChild(t: T) = { ...}
  }

  trait I_CAN_HAS_PARENT[T <: I_CAN_HAS_CHILDREN] {
    def setParent(t: T)
  }


class Parent extends I_CAN_HAS_CHILDREN[Child]

alex

On Thu, Mar 5, 2009 at 11:36 AM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Parent/Child relationships
It's so great to see LOLCat themes.  I can't help but silently LOL.

On Thu, Mar 5, 2009 at 5:02 PM, David Pollak <feeder.of.the.bears@gmail.com> wrote:


On Thu, Mar 5, 2009 at 1:54 PM, Alex Boisvert <boisvert@intalio.com> wrote:
Whoops! I was just playing with the idea and hit the send button.  For entertainment only.

I waz very amuzed. :-)  



On Thu, Mar 5, 2009 at 1:52 PM, Alex Boisvert <boisvert@intalio.com> wrote:
trait LOLParenting {
  type PARENT <: I_CAN_HAS_CHILDRE
  trait I_CAN_HAS_CHILDREN[T <: I_CAN_HAS_PARENT[_]] {
    private List<T> children;
    def addChild(t: T) = { ...}
    def removeChild(t: T) = { ...}
  }

  trait I_CAN_HAS_PARENT[T <: I_CAN_HAS_CHILDREN] {
    def setParent(t: T)
  }


class Parent extends I_CAN_HAS_CHILDREN[Child]

alex

On Thu, Mar 5, 2009 at 11:36 AM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Parent/Child relationships
Use a common class instead, like..

import scala.collection.mutable.Set

class Node(var parent: Node)
{
  protected val nodes = Set[Node]()

  def addNode(node: Node) {
    nodes += node
    node.parent = this
  }
}

On Thu, Mar 5, 2009 at 8:36 PM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés

Rich Dougherty
Joined: 2008-08-20,
User offline. Last seen 2 years 38 weeks ago.
Re: Parent/Child relationships
Hi Andrés

Here's a way to do it with immutable objects using lazy values and call-by-name parameters. I think it works! Does anyone know of a better way?

class Parent(cs: => List[Child]) {
  lazy val children = cs
  def add(data: String): (Parent, Child) = {
    lazy val c: Child = new Child(p, data)
    lazy val p: Parent = new Parent(c :: children)
    (p, c)
  }
}
class Child(p: => Parent, data: String) {
  lazy val parent = p
}

val p1 = new Parent(Nil)
println(p1)
println(p1.add("hello"))


Cheers
Rich

On Fri, Mar 6, 2009 at 8:36 AM, Andrés Testi <andres.a.testi@gmail.com> wrote:
What's the best way to implement a classic parent/child relationship in Scala?
I want a thing like this:

class Parent{

  private List<Child> children;

  public void addChild(Child child){
     child.setParent(this);
     children.add(child);
  }

}

Regards.

- Andrés

--
http://blog.richdougherty.com/

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland