Description
Given this code, the Groovy compiler compiles it and happily inserts a java.lang.Integer into a Map<String, Long. The Java compiler produces a compile-time error for the same code, complaining that 1 is not a Long. The result is the same for static and dynamic compilation: heap pollution.
class Repro { static Map<String, Long> map = new HashMap() static void main(String... args) { println map.computeIfAbsent('key', (__) -> 1) println map.key.class println map.compute('key', (__, Long existing) -> (existing ?: 0L) + 2) } }
import java.util.HashMap; import java.util.Map; class ReproJava { static Map<String, Long> map = new HashMap<>(); static void main(String... args) { map.computeIfAbsent("key", (__) -> 1); map.compute("key", (__, existing) -> (existing != null ? existing : 0L) + 2); } }