groovydsl

Groovy method naming convention in terms of defining our own DSL


I try to create a small DSL, but i'm struggling with even simple stuff. The following script gives me an error.

def DEMON(String input) {
  ['a': input]
}
DEMON 'Hello thingy' a

For some reasons, the parentheses around the parameters are not optional and i get an error. This script runs fine:

def dEMON(String input) {
  ['a': input]
}
dEMON 'Hello thingy' a

Note: the only difference is the lowercase first char. So what is going on here? Why are the scripts interpreted (compiled?) different? Is there some kind of method/class naming schemes i have to follow?

Update: The error message. I guess a Syntax error:

unexpected token: Hello thingy @ line 4, column 7.

Solution

  • The groovy syntax is sometime complex, and the compiler use some rules to choose what it must do. One of this rule is simple : If a word starts with an uppercase, it's probably a class.

    for example, f String is a syntax valid in groovy, and the compiler converts it to f(String.class).

    you can use parenthesis to help groovy understand your DEMON is not a class but a method, DEMON('Hello thingy', a)