How does the Groovy in operator work?

前端 未结 3 1504
花落未央
花落未央 2021-01-11 10:22

The Groovy \"in\" operator seems to mean different things in different cases. Sometimes x in y means y.contains(x) and sometimes it seems to call

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 11:07

    in is the "Membership operator".

    From the documentation for Groovy 3 (emphasis mine):

    8.6. Membership operator

    The membership operator (in) is equivalent to calling the isCase method. In the context of a List, it is equivalent to calling contains, like in the following example:

    def list = ['Grace','Rob','Emmy']
    assert ('Emmy' in list)           # (1)    
    

    (1) equivalent to calling list.contains('Emmy') or list.isCase('Emmy')

    So, Groovy always calls isCase, which in case of a List maps to contains.

提交回复
热议问题