groovy - Groovy GString 中的变量引用

在“Groovy and Grails recipes”一书中,我使用了以下代码片段:

String HelloLanguage = "def hello(language) {return \"Hello $language\"}"

但是,我收到一个编译器错误“您试图引用绑定(bind)中的变量或静态上下文中的实例变量。”因为语言不能被束缚。怎么了?

最佳答案

我对 Groovy 不太熟悉我刚刚在 GroovyConsole 中尝试了你的字符串,但我得到了一个异常 - 在转义美元之后,它运行良好。难道是它?
String HelloLanguage = "def hello(language) {return \"Hello \$language\"}"

https://stackoverflow.com/questions/2536721/

相关文章:

iphone - 我的Xcode-编译错误只是显示红色的感叹号

visual-studio - 在 Release模式下执行构建/重建解决方案时,VS2003错误

compiler-errors - TestNG对我来说很好用,但对其他工程师抛出com.googl

gwt - 使用跨站点支持编译 GWT 代码时出错

compiler-errors - 打包双字节文件名时出错

actionscript-3 - Flash ActionScript 文件突然无法编译?

iphone - 找不到库-命令/Developer/Platforms/iPhoneSimulat

c# - 在VS 2008解决方案中添加了项目;建立依赖性获取编译器错误CS0246

visual-studio-2008 - 从VS2008 Prof迁移到VC++ Express 2

asp.net-mvc - 将生成配置更改为生产时的编译错误-C#MVC