Java 10 allows implicit typing for local variables. You can get a quick refresher here.
Fast forward 6 months, Java 11 will be supporting local variable syntax support for Lambda Parameters in an implicitly typed lambda expression.
Here is an example from the spec:
(var x, var y) -> x.process(y)
With this support, annotations can be added to local variables as they require full type.
(@NotNull var x, @Nullable var y) -> x.process(y)
Expanding the example from my earlier post. Lines #17 and #57 shows this support in action.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.annotations.NotNull; | |
import java.util.Comparator; | |
import java.util.function.ToLongFunction; | |
import java.util.stream.Stream; | |
public class RideProviderTest { | |
@FunctionalInterface | |
private interface RideProvider { | |
long getFareEstimate(String type); | |
} | |
public static void main(String[] args) { | |
// implicitly typed lambda expression | |
ToLongFunction<RideProvider> lyftProvider = (@NotNull var provider) -> provider.getFareEstimate("Lyft"); | |
var lyft = new RideProvider() { | |
@Override | |
public long getFareEstimate(String type) { | |
var estimated_cost_cents_max = 0; | |
switch (type) { | |
case "Line": | |
estimated_cost_cents_max = 3307; | |
break; | |
case "Lyft": | |
estimated_cost_cents_max = 7306; | |
break; | |
case "Plus": | |
estimated_cost_cents_max = 12451; | |
break; | |
case "Premier": | |
estimated_cost_cents_max = 17562; | |
break; | |
case "Lux": | |
estimated_cost_cents_max = 23624; | |
break; | |
case "Lux SUV": | |
estimated_cost_cents_max = 25879; | |
break; | |
} | |
return estimated_cost_cents_max; | |
} | |
}; | |
long lyftFare = lyftProvider.applyAsLong(lyft); | |
// implicitly typed lambda expression | |
ToLongFunction<RideProvider> uberProvider = (@NotNull var provider) -> provider.getFareEstimate("uberX"); | |
var uber = new RideProvider() { | |
@Override | |
public long getFareEstimate(String type) { | |
var estimated_cost_cents_max = 0; | |
switch (type) { | |
case "POOL": | |
estimated_cost_cents_max = 7000; | |
break; | |
case "EXPRESS POOL": | |
estimated_cost_cents_max = 7000; | |
break; | |
case "uberX": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "WAV": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "ASSIST": | |
estimated_cost_cents_max = 9200; | |
break; | |
case "uberXL": | |
estimated_cost_cents_max = 14800; | |
break; | |
case "SELECT": | |
estimated_cost_cents_max = 20900; | |
break; | |
case "SUV": | |
estimated_cost_cents_max = 30600; | |
break; | |
} | |
return estimated_cost_cents_max; | |
} | |
}; | |
long uberFare = uberProvider.applyAsLong(uber); | |
// compare the futures for best fare estimate | |
Stream.of(lyftFare, uberFare) | |
. | |
min(Comparator.comparing(i -> i)) | |
. | |
ifPresent(estimated_cost_cents_max -> System.out.println("$" + estimated_cost_cents_max / 100)); | |
} | |
} |
Java 11 is releasing next month! IntelliJ IDEA 2018.2 has already added support for this feature.