Clojure

Hints don't work with #() form of function

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: Release 1.7
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:
    Screened

Description

Example showing how a local fn can be hinted but an anonymous function cannot:

;; OK
user> (let [ex (java.util.concurrent.Executors/newFixedThreadPool 1)
            f (fn [])]
        (.submit ex ^Runnable f))
nil
;; ERROR - this should work the same as the previous
user> (let [ex (java.util.concurrent.Executors/newFixedThreadPool 1)]
        (.submit ex #()))
CompilerException java.lang.IllegalArgumentException: More than one matching method found: submit, compiling:(/private/var/folders/7r/_1fj0f517rgcxwx79mn79mfc0000gn/T/form-init7901279404687292754.clj:3:9)

Cause: Functions have metadata, but Compiler does not look in them for type hints. Var expressions and local bindings use :tag metadata to override return of getJavaClass(). Compiler parses #() into a FnExpr, which always return AFunction as its class.

Proposed: Change FnExpr.getJavaClass() to return tag as type if it is available.

Patch: clj-1378-v2.diff

Screened by: Alex Miller

  1. clj-1378-v2.diff
    12/Mar/14 2:51 PM
    2 kB
    Jozef Wagner
  2. clj-1378.diff
    12/Mar/14 4:16 AM
    0.8 kB
    Jozef Wagner

Activity

Hide
Jozef Wagner added a comment -

Functions do have metadata, but Compiler does not look in them for type hints.

user=> (with-meta #() {:foo :bar})
#<clojure.lang.AFunction$1@779325ee>

When compiler is determining which native method to use, it matches method signature with classes of given args. There is a getJavaClass() method in Compiler.java which returns a class for given expression. Vars expressions and local bindings use :tag metadata to override this class, but most other expressions don't. Compiler parses #() into a FnExpr, which always return AFunction as its class.

Most of time this approach is OK, as AFunction implements Runnable and Callable so there is no need for type hint. However, in this particular case, there are overrides for both Runnable and Callable, and as AFunction can be either of them, the expression is ambiguous.

Show
Jozef Wagner added a comment - Functions do have metadata, but Compiler does not look in them for type hints.
user=> (with-meta #() {:foo :bar})
#<clojure.lang.AFunction$1@779325ee>
When compiler is determining which native method to use, it matches method signature with classes of given args. There is a getJavaClass() method in Compiler.java which returns a class for given expression. Vars expressions and local bindings use :tag metadata to override this class, but most other expressions don't. Compiler parses #() into a FnExpr, which always return AFunction as its class. Most of time this approach is OK, as AFunction implements Runnable and Callable so there is no need for type hint. However, in this particular case, there are overrides for both Runnable and Callable, and as AFunction can be either of them, the expression is ambiguous.
Hide
Jozef Wagner added a comment -

Patch added, following expression will now run without error

(.submit (java.util.concurrent.Executors/newCachedThreadPool) ^Runnable #())
Show
Jozef Wagner added a comment - Patch added, following expression will now run without error
(.submit (java.util.concurrent.Executors/newCachedThreadPool) ^Runnable #())
Hide
Alex Miller added a comment -

Could you add a test to the patch?

Show
Alex Miller added a comment - Could you add a test to the patch?
Hide
Jozef Wagner added a comment -

Attached patch clj-1378-v2.diff which contains both fix and test.

Show
Jozef Wagner added a comment - Attached patch clj-1378-v2.diff which contains both fix and test.

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated: