views:

227

answers:

2

Hello guys,

in Scala, if I want to implement a DSL, is there a way to do the following:

I have an Object called "Draw" which contains the function def draw(d:Drawable)

how can I make it so that I can import the Object and call it outside the object like:

draw ball

if ball extends the Drawable trait? The problem is that I want to use draw in a kind of infix notation, but I dont want to qualify the function draw by denoting it's implementing class/object.

+2  A: 
Arjan Blokzijl
The problem with your first version is that Scala's "operator notation" needs an explicit receiver, that's why the class example works.
Mirko Stocker
I think draw(ball) could be acceptable syntax. I would have something like draw(ball) at (x,y,z) on someOpenGlPanel. It is just a test of doing a small DSL anyway :) Thanks for your answer.Btw: maybe one could accept some filler symbol like Draw -> ball or whatever...back to thinking :)
Felix
+3  A: 

You can't do it. Aside from four prefix operators, in any operator notation the first token represents the object.

Daniel
Actually, Felix _could_ do it by making `draw` an object and `ball` a method of an anonymous class of an implicit conversion that referenced the real object. I doubt he, or anyone else, would prefer that, so +1 for this answer.
Rex Kerr