I missed such a feature in Scala. But in fact it is possible to add methods and fields on a class with Scalas implicit conversions. Debasish Ghosh explains how to add methods on existing classes.
But how do we add new fields to a class? E.g. we have a class Vertex. This class is used in severall packages. Now we want to layout some vertices in an euclidean plane. In the context of the layouting algortihm every vertex has a position.
def main(args : Array[String]) : Unit = {
val v = new Vertex();
v.x = 2.3;
println(v.x)
}
It would be nice to use a vertex like this without the need to access a map manually to get the additional fields. In this piece of code object creation is in our control, but we assume that the layouting algorithm does not have control over the vertex creation.
The extension Position would look like this:
class Position {
var x = 0.0;
var y = 0.0;
}
The last missing piece is how to delegate the call to x.
object EuclideanExtension {
val extendedVertices = new HashMap[Vertex, Position]
implicit def extendVertex(v : Vertex) : Position = {
if (!extendedVertices.contains(v)) {
extendedVertices += v -> new Position;
}
extendedVertices(v);
}
}
The implicit conversion enables us to use all methods and fields of Position whenever we import the EuclideanExtension. (import graph.EuclideanExtension._;)
It is possible to extract most of the code to an abstract, generic class. So you can reuse this extension mechanism.
abstract class ClassExtension[S, T] {
val extendedObjects = new HashMap[S, T]
implicit def extendObject(o : S) : T = {
if (!extendedObjects.contains(o)) {
extendedObjects += o -> createExtendedObject;
}
extendedObjects(o);
}
def createExtendedObject : T;
}
Using the abstract class reduces the code:
object EuclideanExtension extends ClassExtension[Vertex, Position] {
def createExtendedObject = new Position;
}
No comments:
Post a Comment