Skip to content

Instantly share code, notes, and snippets.

@Alotor
Last active August 29, 2015 14:01
Show Gist options
  • Save Alotor/9b30782aaa49d8c421a2 to your computer and use it in GitHub Desktop.
Save Alotor/9b30782aaa49d8c421a2 to your computer and use it in GitHub Desktop.
Test DSL Evaluator that doesn't work as I expected
class DslEvaluator {
def methodMissing(String name, args) {
println "M >> $name $args"
}
def propertyMissing(String name, args) {
println "P >> $name"
}
}
def clos = {
// This call doesn't call "propertyMissing"
String
// This does
Strin
// This will call "methodMissing"
String {
}
// This will not call neither methodMissing nor propertyMissing
"STRING"
// This will call methodMissing
"STRING"()
}
clos.delegate = new DslEvaluator()
clos.resolveStrategy = Closure.DELEGATE_ONLY
clos()
@ysb33r
Copy link

ysb33r commented May 6, 2014

clos compiles to

        java.lang.Object clos = { 
            java.lang.String
            Strin 
            this.String({ 
            })
            'STRING'
            this.STRING()
        }

Secondly, I am not sure what you mean by existing class

@Alotor
Copy link
Author

Alotor commented May 6, 2014

What I mean by "existing class" is a class in the current scope.

I want "hooks" when the DSL looks like...

def clos = {
   String
   Integer
   Float
}

@melix
Copy link

melix commented May 6, 2014

I don't see any solution apart from an AST transformation. Your issue, here, is basically that String is a valid expression, which returns a Class, so it's like any other piece of code, it compiles and evaluates fine...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment