What does [:] mean in groovy?

后端 未结 3 1704
我在风中等你
我在风中等你 2021-01-03 18:44

While reading some groovy code of another developer I encountered the following definition:

def foo=[:]

What does it mean?

3条回答
  •  花落未央
    2021-01-03 19:22

    Quoting the doc:

    Notice that [:] is the empty map expression.

    ... which is the only Map with size() returning 0. ) By itself, it's rarely useful, but you can add values into this Map, of course:

    def emptyMap = [:]
    assert emptyMap.size() == 0
    emptyMap.foo = 5
    assert emptyMap.size() == 1
    assert emptyMap.foo == 5
    

提交回复
热议问题