In my DSL I want to be able to do like this:
val a = 5
Parse("TYPE") {
a match {
case 3 => info("almost")
case s: String => error("wrong type")
case 5 => info("you won!")
case _ => error("omg")
}
}
with output
[INFO] TYPE: you won!
where Parse is a function object, which has the apply(String), info(String) and error(String) methods:
object Parse {
def apply(name: String)(parseF: => Unit) { parseF }
def info(text: String) { println("[INFO] " + name + ": " + text) }
def error(text: String) { println("[ERROR] " + name + ": " + text) }
}
The trick is that the output of the info and error methods should be somehow native to the Parse object and construct the message by the example as shown above. Therefore, they
- have to be accessible like in the example, without any imports.
- have to have the access to the first argument passed to
Parse()("TYPE" in the example). - must not create any instances or extra objects during their working.
This is an ideal description. I think that it will require some more boilerplate. Please suggest, how can I acheive that?
EDIT: my initial guess to declare the info and error methods inside the apply method does not make them visible in the parseF method being passed. Well, unsurprisingly..