Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created September 4, 2009 13:35
Show Gist options
  • Save NicolasT/180882 to your computer and use it in GitHub Desktop.
Save NicolasT/180882 to your computer and use it in GitHub Desktop.
sealed case class Agent(name: String)
sealed case class Machine(agent: Agent, parent: Machine)
trait Applicable[I, O] {
def apply(a: Applicable[I, I]): Applicable[I, O] = {
val outer_apply: Function1[I, O] = apply
new Applicable[I, O] {
def apply(obj: I): O = outer_apply(a(obj))
}
}
def apply(obj: I): O
}
object agent extends Applicable[Machine, Agent] {
def apply(obj: Machine) = obj.agent
}
object parent extends Applicable[Machine, Machine] {
def apply(obj: Machine) = obj.parent
}
object DSL2 {
def main(args: Array[String]): Unit = {
val m1 = Machine(Agent("agent1"), null)
val m2 = Machine(Agent("agent2"), m1)
val m3 = Machine(Agent("agent3"), m2)
val selector = agent(parent(parent))
println("Name of agent: " + selector(m3).name)
println("Name of agent of m3: " + agent(m3).name)
}
}
// vim: set ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment