Now, if we simply use final List<Double> list = new ArrayList<>();
Rather than final List<? extends Double> list = new ArrayList<>();
Then list.add(d);
works as it should:
import java.util.List;
import java.util.ArrayList;
public class Test {
public final List<Double> list = new ArrayList<>();
public final double d = Math.random();
public Test() {
list.add(d); // now it finally works!
}
@Override public String toString() {
return list.toString();
}
@SafeVarargs public static final void main(final String... args) {
System.out.println(new Test());
}
}
Just found out that final List<? super Double> list = new ArrayList<>();
works too!
Somehow only final List<? extends Double> list = new ArrayList<>();
is problematic.
Oh yeah, I realized I confused these two right there.
Of course, for super I can put Doubles in as I like.
And on the other hand, for extends I can’t put in Doubles for sure but what you get is definitely a Double or inheriting from it.
So
List<? extends Double> L = new ArrayList<>();
Double d = L.get(0);
compiles, though would throw an error at runtime with this exact code only.
I guess I’ll just add it here because it’s from the same corner, but lambda expressions don’t work either.
Lambda expressions are allowed only at source level 1.8 or above
That’s right. I suspect a rule of thumb about the current syntax is “most of 7, bits of 8” – and unfortunately lambdas are not a part of 8 that got got. GoToLoop has been pining for adding lambdas to Processing for … maybe a decade…? Another rule of thumb is the more complex or abstract a Java feature is the less likely it is to have been prioritized for Antlr pre-processor handling. A few things were acquired essentially “for free” because the preprocessor didn’t have to be changed, just pass them through.