# Can Java lambdas reuse their named arguments to remove type declarations?

From what I have seen so far the usual way of scoring in java is by using lambda expressions. I often see Try it online used for it. My actual question comes now:

Can you reuse the name of a named lambda to define a variable, thus allowing you to remove the var? For example, normally you'd do:

f -> {var s = ""; /* rest of the code */}


Are you allowed to make f a String to save a few characters? It'd then look like this:

f -> {f = ""; /* rest of the code */}

• As for the second question, so long as it works as intended (aka, gives the correct output), there's almost no limitations aside from our standard loopholes Aug 19 at 13:07
• That helps a lot, Thanks!! Aug 19 at 13:07
• One more related thing. If my task is to print the string "Foo", I'd not be allowed to pass that string in through the lambda right? That'd be nonsensical. Aug 19 at 13:09
• @Jadefalke no, your answer needs to be self-contained in that respect. Aug 19 at 13:09
• I've removed the first question from the question, as that's the only duplicate. The second question is a legitimate question that I'm not sure has been asked before, so I've changed the focus of the question to that. Feel free to revert any changes you dislike Aug 19 at 13:14
• I would say that the second shouldn't be allowed because although you're not using the value of f, you're taking advantage of its type. The input f shouldn't be mentioned anywhere in your lambda other than the parameter list because it's supposed to not even exist.
– user
Aug 19 at 13:16
• I get your point. That was my struggle in the first place. The previous answer thought it was ok. I'm a little confused right now, especially because I just scored using this method. Aug 19 at 13:33

## 1 Answer

Writing f->{/*code*/} is absolutely alright, as agreed upon in the question Dude coinheringaahing linked.

However, I would say that the second shouldn't be allowed because although you're not using the value of f, you're taking advantage of its type. In my opinion, the input f shouldn't be mentioned anywhere in your lambda other than the parameter list because it's really not even supposed to exist - it's just meant to save a byte over ()->{...}.

• Oh hang on, does Java require you to pass a string to the lambda for the answer to work? I was thinking from a JS perspective where it's work fine if you didn't pass anything. Aug 19 at 13:52
• To the answer: I believe that solves it. I'm gonna go with that now and change my answer! Aug 19 at 13:53
• @BrowncatPrograms Yes (or null, since strings are objects).
– user
Aug 19 at 14:11