大佬教程收集整理的这篇文章主要介绍了string – Groovy:${}内变量的嵌套计算,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
def obj = {["name":"whatever","street":"ABC-Street","zip":"22222"]} def fieldNames = ["name","street","zip"] fieldNames.each{ fieldname -> def result = " ${{->"obj.${fieldnamE}"}}" //can't figure out how this should look like println("Gimme the value "+result); }
结果应该是:
Gimme the value whatever Gimme the value ABC-Street Gimme the value 22222
我试图解决这个问题要么没有给出正确的结果(例如只是obj.street),要么根本不会编译.
到目前为止,我似乎还没有理解整个概念.但是,看到这个:http://groovy.codehaus.org/Strings+and+GString我相信它应该是可能的.
AFAIK默认是不可能的; ${}中的表达式不会被重新评估.无论如何,这将是危险的,因为它非常容易使它无限深入并且吹嘘堆栈.
在这种情况下,无论如何都没有必要.
>使obj成为一个实际的地图,而不是一个闭包,和
>对字段名称使用法线贴图[]访问
def obj = [ "name": "whatever","street": "ABC-Street","zip": "22222" ] def fieldNames = ["name","zip"] fieldNames.each { println "Gimme the value ${obj[it]}" } Gimme the value -> whatever Gimme the value -> ABC-Street Gimme the value -> 22222
编辑我可能误解了你并且故意让obj成为闭包而不是地图,尽管我不明白为什么.
以上是大佬教程为你收集整理的string – Groovy:${}内变量的嵌套计算全部内容,希望文章能够帮你解决string – Groovy:${}内变量的嵌套计算所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。