Groovy Operator Overloading Boxing of Java Primitives

I’m trying to make use of Groovy’s Operator Overloading, to use methods on my Java class in a Groovy context (a GroovyShell):

My class is, effectively, an ordered map of keys and values, so the interface looks something like this:

public interface GroovyMap<K, V> {
  // gets the value associated with the given key
  V getAt(K key);
  // gets the nth value, at the given index, in the ordered sequence of values
  V getAt(int index); 

The getAt(K key) method is working fine in Groovy:

GroovyMap<LocalDate, Double> map = ...
K key = ...
Double value = map[key];

However, the getAt(int index) method does not work in Groovy – i.e.:

GroovyMap<LocalDate, Double> map = ...
int i = 0;
Double value = map[i];

will throw a CastClassException:

java.lang.Integer cannot be cast to javax.time.calendar.LocalDate

I’m not entirely sure what’s going on here:

  • Groovy has some funky behaviour with respect to numbers and primitives (e.g. 0.0 is effectively interpreted as new BigDecimal("0.0")), but I would have thought the explicitly defining i as an int would resolve that.
  • It looks like the Groovy Operator Overloading is boxing any primitive values, and as such it’s trying to pass Double to what is effectively the getAt(LocalDate key) method. However, I can’t find any documentation to that effect. If that’s the case, can you direct me to any such?
  • Is there something else going on here that I’m not aware of?