- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Parent/Child relationships
Thu, 2009-03-05, 20:37
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
Thu, 2009-03-05, 22:57
#2
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:
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
Thu, 2009-03-05, 23:07
#3
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:
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
Thu, 2009-03-05, 23:07
#4
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
Fri, 2009-03-06, 00:07
#5
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 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
Fri, 2009-03-06, 00:27
#6
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:
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
Fri, 2009-03-06, 10:47
#7
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:
--
http://blog.richdougherty.com/
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/
2009/3/5 Andrés Testi <andres.a.testi@gmail.com>
--
Thanks,
-Vlad