Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a lambda expression when parameters are ignored in the body

How do I write a lambda expression if it doesn't require arguments and hence its name is excessive?

This way doesn't compile:

setRowFactory(-> new TableRowCustom());

But this one does:

setRowFactory(__ -> new TableRowCustom());

Is there a better way?

like image 977
Dims Avatar asked Jun 02 '16 16:06

Dims


People also ask

Can lambda expression have empty body?

You can think of lambda expressions as anonymous methods (or functions) as they don't have a name. A lambda expression can have zero (represented by empty parentheses), one or more parameters. The type of the parameters can be declared explicitly, or it can be inferred from the context.

Can we write parameters less lambda expression?

No, there isn't. Lambda expressions are optimised (in terms of syntax) for the single parameter case.

Is it mandatory to declare type of parameter in lambda expression?

A lambda expression is characterized by the following syntax. Following are the important characteristics of a lambda expression. Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.

Can we have exception inside a lambda expression?

A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.


1 Answers

Since you've mentioned that this works

setRowFactory(__ -> new TableRowCustom());

I assume that the expected functional interface method must accept a single argument. The identifier _ is a reserved keyword since Java 8.

I would just use a throwaway single (valid identifier) character.

setRowFactory(i -> new TableRowCustom());
setRowFactory($ -> new TableRowCustom()); 

Although, you should probably avoid the use of the $, the Java Language Specification states

The dollar sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems. The underscore may be used in identifiers formed of two or more characters, but it cannot be used as a one-character identifier due to being a keyword.

You can also write out

setRowFactory(ignored -> new TableRowCustom());

to be explicit.

The Java Language Specification defines the syntax of a lambda expression

LambdaExpression:
  LambdaParameters -> LambdaBody 

and

LambdaParameters:
  Identifier
  ( [FormalParameterList] )
  ( InferredFormalParameterList )
InferredFormalParameterList:
  Identifier {, Identifier}

In other words, you cannot omit an identifier.


As Holger suggests, if and when they decide to use _ as an unused parameter name, it will be easy to change from __ to _ in your source code. You may want to just stick with that for now.

like image 88
Sotirios Delimanolis Avatar answered Oct 08 '22 00:10

Sotirios Delimanolis