Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline lambda

I have this code, which works:

  new JdbcTemplate(new SingleConnectionDataSource(c, true))
        .query("select id, name from PLAYERS", (rs, rowNum) ->
            new Player(rs.getString("id"), rs.getString("name")) // oneline
        );

However I now need to add multiple statements in the new Player() part. I tried enclosing them in brackets, but it doesn't seem to work. What's the correct syntax?

like image 389
BobJI Avatar asked Feb 11 '20 09:02

BobJI


People also ask

Can a lambda function be multiple lines?

No, you can't write multiline lambda in Python because the lambda functions can have only one expression. The creator of the Python programming language – Guido van Rossum, answered this question in one of his blogs. where he said that it's theoretically possible, but the solution is not a Pythonic way to do that.

How do you write a multiline lambda?

No, you cannot write multiple lines lambda in Python. The lambda functions can have only one expression. One logical answer to this question is that if you want to write multiline functions, then you should use regular functions in Python; why do you go for multiline lambdas.

How do you write a multiline lambda in Java?

In Java 8, it is also possible for the body of a lambda expression to be a complex expression or statement, which means a lambda expression consisting of more than one line. In that case, the semicolons are necessary. If the lambda expression returns a result then the return keyword is also required.

Do lambda functions have to be one line?

In Python, a lambda function is a single-line function declared with no name, which can have any number of arguments, but it can only have one expression. Such a function is capable of behaving similarly to a regular function declared using the Python's def keyword.


2 Answers

I'm assuming the method of the functional interface implemented by this lambda expression has a return value, so when using brackets, it should include a return statement, just like any method with non-void return type.

new JdbcTemplate(new SingleConnectionDataSource(c, true))
    .query("select id, name from PLAYERS", (rs, rowNum) ->
        {
            return new Player(rs.getString("id"), rs.getString("name");
        }) 
    );
like image 184
Eran Avatar answered Sep 27 '22 23:09

Eran


Don't do that. Having multiple statements in a lambda in most cases is a code smell. Rather create a method with two parameters:

private Player toPlayer(ResultSet rs, int rowNum) {
    // multiple setters here
    return player;
}

And then pass the method reference (which in fact will behave like a BiFunction) instead of lambda:

new JdbcTemplate(new SingleConnectionDataSource(c, true))
    .query("select id, name from PLAYERS", this::toPlayer);

One may want to create a static utility method instead of a dynamic one. The logic is the same as above:

public class MappingUtil {
    // ...
    public static Player toPlayer(ResultSet rs, int rowNum) {
        // multiple setters here
        return player;
    }
}

And then:

new JdbcTemplate(new SingleConnectionDataSource(c, true))
    .query("select id, name from PLAYERS", MappingUtil::toPlayer);
like image 38
ETO Avatar answered Sep 27 '22 23:09

ETO