1

I have the following closure: def check = {x,y,z -> some code}

Is it possible to get the parameters names("x","y","z") inside the closure? and to use it as part of the closure code

For example to print arg name and then it's value.

Thanks

roh
  • 123
  • 1
  • 1
  • 10
  • Within the closure you know what their names are. But if from outside the closure you want to know what all arguments are being passed to it and their values, then you can't do that. – Sandeep Poonia May 31 '16 at 14:30
  • I want to use them within the closure. For example: def check = {x,y -> println **arg1Name + x} – roh Jun 01 '16 at 05:13
  • There is no direct way for doing this. See http://stackoverflow.com/q/4114190/1799527 – Sandeep Poonia Jun 01 '16 at 09:33

1 Answers1

0

You can use of course these parameters in closure.

def a = {a, b, c ->
    a + b + c
}
assert 6 == a(1,2,3)

Second version

def a = {a, b, c ->
    [a:a, b:b, c:c]
}
assert [a:1, b:2, c:3] == a(1,2,3)
koji
  • 179
  • 6