groovy
  1. groovy
  2. GROOVY-6044

Add handler for "Cannot return value of type Foo on method returning type Bar" error

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2.0-beta-1
    • Component/s: Static Type Checker
    • Labels:
      None
    • Number of attachments :
      0

      Description

      The "Cannot return value of type Foo on method returning type Bar" at the moment cannot be handled by typechecking extensions scripts.

      It, alas, does happen with otherwise perfectly valid code like e.g.,

      def valueForKey(key) { // can return effectively anything
        myLocalStorageMap[key]
      }
      Map foo() { // here we happen to know a Map always comes out
        valueForKey('thisKeyDoesAlwaysRepresentAMap')
      }
      

      Of course, adding an explicit typecast into the source helps. But I've thought it's the typechecking extension very raison d'├¬tre to allow such things to go trough the typechecker without having to typecast explicitly in the code – the extension would say to the typechecker essentially "Yup, I know, it's all right here, let it pass".

      The priority is minor since there are other possible work-arounds (namely, pusing the error collector and filtering these errors out later). Nevertheless direct support by a specific handler would be much better, for there would be more contextual information at the moment.

        Activity

        Hide
        CÚdric Champeau added a comment -

        The type checking extensions now support a new handler:

        incompatibleReturnType { returnStmt, inferredReturnType ->
            if (inferredReturnType==STRING_TYPE) {
                handled = true
            }
        }
        
        Show
        CÚdric Champeau added a comment - The type checking extensions now support a new handler: incompatibleReturnType { returnStmt, inferredReturnType -> if (inferredReturnType==STRING_TYPE) { handled = true } }

          People

          • Assignee:
            CÚdric Champeau
            Reporter:
            OC
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: