In my last post I showed how to define a JCR tree structure with Scala. I introduced two new operators, which allow the code to be written such that it resembles an ASCII art drawing of the tree. In this post I discuss the goals I had in mind when designing these operators. This will help to understand how these operators actually work and serve as preparation for a later post where I will show how properties fit into the picture.
My goals were:
- To design an integrated mechanism for building JCR tree structures in an expressive and concise way. The code should resemble the tree structure as close as possible.
- Don't use esoteric magic. That is, the mechanism should be a clean, rigorous abstraction, which does not expose strange artefacts. It should be applicable without having to deal with its internals while still being relatively easy to comprehend if necessary.
I think I reached the first goal. ASCII art wasn't my primary intent. Rather did it turn out that way after dismissing various other approaches most of which grossly violated the second goal. The second goal is more interesting and deserves a more detailed explanation. Along the way I explore the details of how these operators work and which Scala features they employ.
The ¦- operator is for adding nodes to a parent node:
n ¦- "child1"
n ¦- ("child2", "nt:file")
The first line adds a node called 'child1' of type nt:unstructured to the parent node n. The second line adds a node called 'child2' of type nt:file to the same node n. There are several notable points here: First (more or less) arbitrary symbols can be used as identifiers in Scala. So ¦- is just a fancy name for a method. Further Scala allows for infix notation. Removing syntactic sugar reveals plain old method calls on n.
n.¦-("child1")
n.¦-("child2", "nt:file")
But wait, n is of type Node and Node does not have a method called ¦-. At this point Scala's Pimp my library design pattern is used to retroactively extend the Node class with additional methods. (This is similar to extension methods in C#. See also my post JCR with Scala for further details).
implicit def extendNode(node: Node) =
new NodeExtender(node)
protected class NodeExtender(node: Node) {
( ... )
def -+[T](f: Node => T) = f(node)
def ¦-(relPath: String) = node.addNode(relPath)
def ¦-(relPath: String, tyqe: String) =
node.addNode(relPath, tyqe)
}
With this extensions in place ¦- can be called on a Node instance as if it were an actual method of the Node class. From the code we also see, that ¦- is just an alias for Node's addNode method.
Let's turn to the second operator +- now. It is used for adding new branches:
root ¦- "Movies" -+ { n: Node =>
n ¦- "Pulp Fiction"
n ¦- "Casablanca"
n ¦- "The Godfather"}
This code first adds a child node 'Movies' to the 'root' node. It then adds the three nodes 'Pulp Fiction', 'Casablanca', and 'The Godfather', respectively to the 'Movies' node. Let's again de-sugar it:
root.¦-("Movies").-+({ n: Node =>
n ¦- "Pulp Fiction"
n ¦- "Casablanca"
n ¦- "The Godfather"})
First the ¦- operator is called on root passing the string "Movies" as argument. Then the +- operator is called on the node returned by ¦- passing an anonymous function as argument. Taking a look at the definition of the +- operator reveals the remaining details on what is going on. +- takes as argument a function, which takes an argument of type Node and returns an arbitrary type T. It then applies that function to the node +- was called on. So here the 'Movies' node is passed as argument for the parameter n of the anonymous function. The latter eventually adds the three nodes 'Pulp Fiction', 'Casablanca', and 'The Godfather' by invoking ¦- on n.
So while there is some magic involved here, I think it is definitely not esoteric. The operators are implemented by means of standard Scala features and patterns. There are next to none side effects. Although, leveraging Scala's implicits might result in unwanted automatic conversions in rare cases, that risk can be minimized by carefully restricting the scope of the Node extensions.

Vizyondaki Filmler | Fragman izle | Sinema