[e-lang] facet problem

Martin Scheffler martinscheffler at googlemail.com
Sun Jan 21 14:02:34 CST 2007


Hi, I have a problem with using facets:
I have a class node. It lets me add and remove child-nodes.
Now in another part of my system I only have access to facets of these nodes.
I want to be able to remove child-nodes by providing the facet of the
child node to the parent node - parentFacet.removeChild(childFacet).
How can the parent node figure out if the facet points to one of its
children?


This code should clarify my problem:

#------------------------8<-------------------------------
pragma.syntax("0.9")

def makeNode(name) :any {

	def children:=[].diverge()

	def node {
		
		to addChild(c) :void {
			children.push(c)
		}
		
		to removeChild(c) :void {
			def pos:=children.indexOf1(c)
			if(pos==-1) {
				throw(`Could not find child`)
			}
			children.removeRun(pos,pos+1)
		}
		
		to print() :void {
			print(`Node $name [ `)
			for child in children {
				child.print()
			}
			print(`]`)
			
		}
		
		to dangerous() :void {
			println("DANGEROUS STUFF")
		}
	}	
	return node
}

def makeNodeFacet(node) :any {
	def nodeFacet {
		to addChild(c) {node.addChild(c)}
		to removeChild(c) {node.removeChild(c)}
		to print() {node.print()}
	}
	return nodeFacet
}

def n1:=makeNode("parent")
def n2:=makeNode("child1")
n1.addChild(n2)

# in another part of the system:
def n1facet:=makeNodeFacet(n1)
def n2facet:=makeNodeFacet(n2)
n1facet.print()

n1facet.removeChild(n2facet)
# problem: Could not find child


More information about the e-lang mailing list