Wednesday, 11 September 2013

Scala Advanced Type Usage

Scala Advanced Type Usage

The Background
I'm working on an event library in Scala. In my library you can define
events like this:
val e1 = new ImperativeEvent[Int]
You can trigger them like this:
e1(42)
You can create reactions like this:
val r1 = (i: Int) => println(i)
And attach them to the event like this:
e1 += r1
There is also some other stuff (like event transformations, compositions
etc). I use the Esper CEP engine as the backend of my library. Esper uses
an SQL-like language called EPL for most operations.
The Problem
I'm trying to implement some more advanced concepts like event joins. So
now you can define events with multiple properties like this (using tuple
types):
val e2 = new ImperativeEvent[(Int, String)]
And then join them like this:
val e3 = e1 join e2 windowLength (30) on "E1.P1 = E2.P1"
which performs a join of e1 and e2 on the last 30 occurrances of both on
the condition that their respective first properties are equal.
This is alright but I'd like to get rid of the strings in my
implementation to make the event expressions type checkable. I'd like to
change the join expression to something like this:
val e3 = e1 join e2 windowLength (30) on e1._1 === e2._1
similar to the way it is done in eg. Squeryl. The problem with this is, I
can't access the types of the elements of the tuple type...
The Question
How can I access the tuple types statically? Right now I've only managed
to access them at run-time through reflection which does not help me. I'm
pretty sure that what I want to achieve is not possible with tuples but
I'm wondering if using HLists from the shapeless library or something
similar might help in achieving my goal.

No comments:

Post a Comment