Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9803

Static type checker forgets nested generic types

    XMLWordPrintableJSON

Details

    Description

      When using a nested generic type (for example, from Vavr Try<Option<T>), Groovy forgets the value of the nested type parameter, simply tagging Object instead of T.

      import io.vavr.control.Option;
      import io.vavr.control.Try;
      
      public class GenericInJava {
          public static void main(String[] args) {
              Try.success(123)
                      .map(Option::of)
                      .map(o -> o.get().intValue()); // happy; correctly identifies o as Option<Integer>
          }
      }
      
      import groovy.transform.CompileStatic
      import io.vavr.control.Option
      import io.vavr.control.Try
      
      @CompileStatic
      class GenericInGroovy {
          static void main(String[] args) {
              Try.success(123)
                  .map(Option::of)
                  .map(o -> o.get().intValue()) // produces error below
          }
      }
      
      Groovy:[Static type checking] - Cannot find matching method java.lang.Object#intValue(). Please check if the declared type is correct and if the method exists.
      

      Explicitly inserting a type witness .<Option<Integer>>map(Option::of) succeeds. Oddly enough, using .map({ Option.of(it) }) also works (maybe it's an interaction glitch involving method references specifically?).

      https://www.javadoc.io/doc/io.vavr/vavr/0.10.4/io/vavr/control/Try.html#map-java.util.function.Function-

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              chrylis Christopher Smith
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1.5h
                  1.5h