Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add Infinite nested maps tip

...

Code Block
languagegroovy
import java.beans.*
def map = new ObservableMap({!(it instanceof Closure)})
map.addPropertyChangeListener({ evt ->
   println "${evt.propertyName}: ${evt.oldValue} -> ${evt.newValue}"
} as PropertyChangeListener)
def bean = new Expando( map )
bean.lang = 'Groovy'  // prints lang: null -> Groovy
bean.sayHello = { name -> "Hello ${name}" } // prints nothing, event is skipped
assert 'Groovy' == bean.lang
assert 'Hello Groovy' == bean.sayHello(bean.lang)

Infinite nested maps tip

With the help of withDefault we can easily create inifinite nested maps structure :

Code Block
languagegroovy
Map infiniteNestedMaps = { [:].withDefault{ owner.call() } }()
        
infiniteNestedMaps.a.b.c.d.e.f.g = 42
        
assert infiniteNestedMaps.a.b.c.d.e.f.g == 42