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

STC doesn't coerce GString in certain ternary expressions

    XMLWordPrintableJSON

Details

    Description

      I know there are a number of edge cases regarding automatic coercion from GString to String, so I'm not certain whether this is a specific case of a larger problem area, but I've been able to isolate it.

      When using the named-parameter constructor approach, ternary expressions don't "inherit" the target type, and thus the inferred type of the expression is Serializable (not even CharSequence!). The problem is identical whether elvis or the long form is used.

      @CompileStatic
      class Elvis {
          static class HasString {
              String value
          }
      
          void foo(String a, String b, String c) {
              new HasString(
                  value: (a ?: "$b $c")
              )
          }
      }
      
      Cannot assign value of type java.io.Serializable <? extends java.lang.Object> to variable of type java.lang.String
      

      This doesn't fail when used as

      new HasString().value =
      

      An explicit as String makes everyone happy but shouldn't be needed.

      Attachments

        Activity

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 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 - 0.5h
                0.5h