开发者

How does this work? Groovy Map creation with nested closures.

开发者 https://www.devze.com 2023-03-23 10:28 出处:网络
I\'m rather new to Groovy, and recently created a canned message using a Map for testi开发者_如何学Cng. I created it using closures (not fully understanding, that\'s what I did, and changed it to the

I'm rather new to Groovy, and recently created a canned message using a Map for testi开发者_如何学Cng. I created it using closures (not fully understanding, that's what I did, and changed it to the standard Map notation). Now I want to understand why the closure notation worked.

So the following two Maps get created properly. My question is how is Groovy interpreting the nested closures into a map?

Map notation:

Map m = [
  person : [
    first : "Flo",
    middle : "Over",
    last : "Stack"
  ],
  address : [
    street1 : "123 Any Street",
    street2 : "2nd Floor",
    city : "Anytown",
    state : "YR",
    zip : "99999"
  ]
]

Closure Notation:

Map m = {
  person {
    first "Flo"
    middle "Over"
    last "Stack"
  }
  address {
    street1 "123 Any Street"
    street2 "2nd Floor"
    city "Anytown"
    state "YR"
    zip "99999"
  }
}


Groovy has a special concept for creating tree structures called Builder. You can find more infos here: http://groovy.codehaus.org/Builders

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号