
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 WhateverGimme the value ABC-StreetGimme 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 -> WhateverGimme the value -> ABC-StreetGimme the value -> 22222 编辑我可能误解了你并且故意让obj成为闭包而不是地图,尽管我不明白为什么.
总结以上是内存溢出为你收集整理的string – Groovy:${}内变量的嵌套计算全部内容,希望文章能够帮你解决string – Groovy:${}内变量的嵌套计算所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)