When I try to create internal DSLs in Scala, I run into a common problem and I could not create a solution. To make everything look like a typical language, I would like the syntax to look something like this:
model 'Foo { decl 'Real 'x; decl 'Real 'y; }
In practice, there are several problems. The first problem is getting the model object here to take two arguments this way. If anyone has any ideas let me know. But what I did was to do something more like this:
model('Foo) { ... }
If the model is now a function, which then returns the object using the apply method, which then consumes the lambda that follows it. I can live with that. I could live with a similar problem inside the lambda, so things like decl 'Real 'x or decl('Real,'x) inside. But what I want to do is get the results of all these expressions inside squiggly braces to get a βreturnβ as a list. In other words, I want to write something like this:
model 'Foo { decl('Real,'x); decl('Real,'y); }
where decl(...) is of type Declaration , and {...} is List[Declaration] . I suspect there is a way to use implicits for this, but I could not find it. In short, I would like to do:
model 'Foo { decl('Real,'x); decl('Real,'y); }
... evaluate the equivalent ...
model 'Foo { decl('Real,'x) :: decl('Real,'y) :: Nil }
Comments or suggestions?
scala dsl
Michael tiller
source share