What's wrong with Groovy multi-line String?

后端 未结 3 892
醉酒成梦
醉酒成梦 2020-12-02 16:31

Groovy scripts raises an error:

def a = \"test\"
  + \"test\"
  + \"test\"

Error:

No signature of method: java.lang.String.         


        
3条回答
  •  时光说笑
    2020-12-02 16:45

    You can tell Groovy that the statement should evaluate past the line ending by adding a pair of parentheses ( ... )

    def a = ("test"
      + "test"
      + "test")
    

    A second option is to use a backslash, \, at the end of each line:

    def a = "test" \
      + "test" \
      + "test"
    

    FWIW, this is identical to how Python multi-line statements work.

提交回复
热议问题