Groovy equivalent for Scala implicit parameters

北战南征 提交于 2019-12-06 07:43:14

You can use closures with a default parameter:

doSomethingWith = { i = value -> println "Got $i" }
value = 5

doSomethingWith(6)  // Got 6
doSomethingWith()   // Got 5

value = 0
doSomethingWith()   // Got 0

This is how I do implicits in Groovy

@Test
def void customString() {
    println "Welcome implicits world in groovy".upperCaseNoSpace
    println "Welcome implicits world in groovy".removeSpaces

}

static {
    String.metaClass.getUpperCaseNoSpace = {
        delegate.toUpperCase().replace(" ", "_")
    }

    String.metaClass.getRemoveSpaces = {
        delegate.replace(" ", "")
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!