Attempting to compile the following code with java-1.21.0-openjdk-amd64 results in these errors
.../TransactionProfile.java:[60,22] variable emvData might already have been assigned
.../TransactionProfile.java:[61,21] variable emvData might already have been assigned
.../TransactionProfile.java:[63,3] variable emvData might not have been initialized
public abstract class TransactionProfile {
protected DeviceContext context;
protected PeripheralResponse response;
protected final PeripheralMessage emvData;
protected final transient PeripheralTags peripheralTags = PeripheralTags.getInstance();
TransactionProfile(DeviceContext context, PeripheralResponse response) {
this.context = context;
this.response = response;
if (response == null) {
emvData = new PeripheralMessage(new LinkedHashMap<>());
} else {
response.get(peripheralTags.emvDataTag)
.ifPresentOrElse(
emv -> emvData = new PeripheralMessage(emv.getUniqueMapOfChildren()),
() -> emvData = new PeripheralMessage(new LinkedHashMap<>()));
}
}
When the constructor is lambda-free it compiles fine
TransactionProfile(DeviceContext context, PeripheralResponse response) {
this.context = context;
this.response = response;
Tlv emv = null;
if (response != null) {
emv = response.get(peripheralTags.emvDataTag).orElse(null);
}
if (emv == null) {
emvData = new PeripheralMessage(new LinkedHashMap<>());
} else {
emvData = new PeripheralMessage(emv.getUniqueMapOfChildren());
}
}
How is emvData "definitely assigned" in the second form and not the first? As far as I can see there is no alternative. https://docs.oracle.com/javase/specs/jls/se15/html/jls-16.html
getEmvDataFromThing, if you think the lambda version is more readable (which I don't). Would require rewriting it a bitifPresentOrElse()will only execute one of its arguments, it cannot know that the finalemvDatawill not be assigned twice.new PeripheralMessage(response.get(peripheralTags.emvDataTag).map(Tlv::getUniqueMapOfChildren).orElse(new LinkedHashMap<>())(I am guessing since no minimal reproducible example was posted)