javagenericstypescastingvavr

Coerce type without creating temporary variable


I have the following function:

  <K, P, Q> Map<K, List<Q>> convert(Map<K, ? extends List<? extends P>> input) {
    return HashMap.empty();
  }

I am calling it in the following way:

Map<Integer, List<? extends String>> stringMap = HashMap.empty();
Map<Integer, List<? extends Double>> doubleMap = HashMap.empty();
Map<Integer, List<Boolean>> stringMapConverted = convert(stringMap);
Map<Integer, List<Boolean>> merged = stringMapConverted.merge(convert(doubleMap));

However, I want to do this without having to create a temporary variable first.
Something like this:

Map<Integer, List<? extends String>> stringMap = HashMap.empty();
Map<Integer, List<? extends Double>> doubleMap = HashMap.empty();
Map<Integer, List<Boolean>> merged = convert(stringMap).merge(convert(doubleMap));

However, I am getting a compilation error here:

'merge(io.vavr.collection.Map<? extends java.lang.Integer,? extends io.vavr.collection.List<java.lang.Object>>)' in 'io.vavr.collection.Map' cannot be applied to '(io.vavr.collection.Map<java.lang.Integer,io.vavr.collection.List<? extends java.lang.Double>>)'

Required type: Map<? extends Integer,? extends List<Object>>
Provided: Map<Integer,List<? extends Double>>

I also tried casting explicitly:

Map<Integer, List<Boolean>> merged2 = ((Map<Integer,List<Boolean>>)convert(stringMap)).merge(doubleMap);

but its throwing a compilation error:

Inconvertible types; cannot cast 'io.vavr.collection.Map<java.lang.Integer,io.vavr.collection.List<java.lang.Object>>' to 'io.vavr.collection.Map<java.lang.Integer,io.vavr.collection.List<java.lang.Boolean>>'

The signature of Map.merge() is as follows:

Map<K, V> merge(Map<? extends K, ? extends V> that);

The Map and List classes are from vavr.


Solution

  • Use type witness, or so called TypeArguments mentioned in 15.12 in SE11 JLS to specify the type argument when invoking convert.

    MethodInvocation:
      MethodName ( [ArgumentList] )
      TypeName . [TypeArguments] Identifier ( [ArgumentList] )
      ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
      Primary . [TypeArguments] Identifier ( [ArgumentList] )
      super . [TypeArguments] Identifier ( [ArgumentList] )
      TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )
    ArgumentList: Expression {, Expression}

    Note that we cannot omit the instance name->(this) when using TypeArguments.

    Map<Integer, List<Boolean>> merged = this.<Integer, String, Boolean>convert(stringMap).merge(convert(doubleMap));