2

When using lambda function variable, is there an alternative to invoking function with apply.

Function<String, String> mesgFunction =  (name) -> "Property "+ name +" is not set in the environment";
Optional.ofNullable(System.getProperty("A")).orElseThrow(() -> new IllegalArgumentException(mesgFunction.apply("A")));
Optional.ofNullable(System.getProperty("B")).orElseThrow(() -> new IllegalArgumentException(mesgFunction.apply("B")));

Is there a shorter syntax to mesgFunction.apply("A"). I tried mesgFunction("A") which complained that the method does not exist. Am I missing something? Isn't there a shorter alternative?

2
  • This doesn't seem like a good use case for a lambda. Is this what you're really trying to do? Why not just put everything on the right side of the -> in the println method directly? Commented Sep 20, 2016 at 21:20
  • There's hope since Scala actually does that. So maybe in a future release we get the shorthand version of methods calls on lambdas. Commented Sep 21, 2016 at 13:02

2 Answers 2

4

No, the fact that an interface is a functional interface doesn’t allow any alternative calling syntax; it’s methods are invoked like any other interface method.

But you can just factor out more of the common code to shorten the repeated code

Function<String, Supplier<IllegalArgumentException>> f = name ->
    () -> new IllegalArgumentException("Property "+name+" is not set in the environment");
String valueA = Optional.of("A").map(System::getProperty).orElseThrow(f.apply("A"));
String valueB = Optional.of("B").map(System::getProperty).orElseThrow(f.apply("B"));

however, this still has no advantage over a conventional

public static String getRequiredProperty(String name) {
    String value = System.getProperty(name);
    if (value == null) {
        throw new IllegalArgumentException("Property "+name+" is not set in the environment");
    }

    return value;
}

 

String valueA = getRequiredProperty("A");
String valueB = getRequiredProperty("B");

which has the advantage of having no code duplication (esp. regarding the constants "A" and "B"), thus less room for accidental inconsistencies.

Sign up to request clarification or add additional context in comments.

7 Comments

Why not just have Function<String, String> getRequiredProperty = (name) -> Optional.ofNullable(System.getProperty(name)).orElseThrow(() -> new IllegalArgumentException("Property "+ name +" is not set in the environment"));? Then the invocation simply becomes String valueA = getRequiredProperty.apply("A").
Say yourself… what’s the advantage of saying getRequiredProperty.apply("A") over getRequiredProperty("A")?
For your first answer, The duplication of code is reduced by eliminating the need to specify the property name twice.
Yes, it’s better than your original code, but it still has no advantage over an ordinary method.
@JacobvanLingen the body of a lambda expression ends up in a private method of the owner class. You’d need a bit of guesswork or heuristic to find the right method when multiple functions with the same signature exist, but it’s still possible to invoke it via Reflection.
|
2

No, there is no alternative like that. apply() is "just" a method of the Function interface, so that method must be called. There is no syntactic sugar to make that more concise.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.