0

我有这个 groovy 脚本,它定义了一个正常工作的闭包。

escape = { str -> 
    str.collect{ ch ->
        def escaped = ch
        switch (ch) {
            case "\"" : escaped = "\\\"" ; break
            // other cases omitted for simplicity
        }
        escaped
    }.join()
}

assert escape("\"") == "\\\""       //Sucess

但是当我向脚本添加另一个使用 GString 插值的闭包时。

escape = { str -> 
    //Same as above
}

dummy = {
    aStr = "abc"
    "123${aStr}456"
}

//Compilation fails

我得到错误

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script650.groovy: 7: expecting anything but ''\n''; got it anyway @ line 7, column 39.
        case "\"" : escaped = "\\"" ; break
                                 ^

1 error

即使添加的闭包被评论。

escape = { str -> 
    //Same as above
}

/*dummy = {
    aStr = "abc"
    "123${aStr}456"
}*/

//Compilation fails

还是失败!是什么赋予了?

4

0 回答 0