How do I get the output of a closure without assigning it to a variable?

Viewed 49

I want to write this as a single line and not have to do a variable assignment:

def output = myclosure [sdfsdf:"sdfsdf"] // should return "somestring"
assert output == "somestring"

I can't figure out the syntax to just do something like this: assert myclosure: "sdfsdfds" == "somestring"

1 Answers

You can evaluate the closure without assigning its output to a variable:

    def aClosure = {
        "somestring"
    }
    assert aClosure() == "somestring"

    assert {"somestring"}() == "somestring"

    def bClosure = {param ->
        param = "somestring"
    }
    assert bClosure("whatever") == "somestring"
    assert bClosure() == "somestring"

    assert {param -> "somestring"}() == "somestring"
Related