Uploaded image for project: 'Clojure'
  1. CLJ-1232

Functions with non-qualified return type hints force import of hinted classes when called from other namespace

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Completed
    • Affects versions: Release 1.7, Release 1.6, Release 1.5
    • Fix versions: Release 1.8
    • Approval:
      Ok
    • Patch:
      Code and Test

      Description

      You can add a type hint to function arglists to indicate the return type of a function like so.

      user> (import '(java.util List))
      java.util.List
      user> (defn linkedlist ^List [] (java.util.LinkedList.))
      #'user/linkedlist
      user> (.size (linkedlist))
      0
      

      The problem is that now when I call `linkedlist` exactly as above from another namespace, I'll get an exception because java.util.List is not imported in there.

      user> (in-ns 'user2)
      #<Namespace user2>
      user2> (refer 'user)
      nil
      user2> (.size (linkedlist))
      CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: List, compiling:(NO_SOURCE_PATH:1:1)
      user2> (import '(java.util List)) ;; Too bad, need to import List here, too.
      java.util.List
      user2> (.size (linkedlist))
      0
      

      There are two workarounds: You can import the hinted type also in the calling namespace, or you always use fully qualified class names for return type hints. Clearly, the latter is preferable.

      Approach: Resolve the tags in the defn macro.

      Patch: 0001-CLJ-1232-auto-qualify-arglists-class-names.patch

      Screened by: Fogus

      Alternate approach: Make the compiler resolve the return tags when necessary (tag is not a string, primitive tag (^long) or array tag (^longs)) and update the Var's :arglist appropriately. Patch: 0001-auto-qualify-arglists-class-names-v4.patch. Note that this patch had problems with Hystrix, which was using non-conformant arglists - Hystrix has since been patched.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              tsdh Tassilo Horn
            • Votes:
              7 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: