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

Description

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

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

Environment

None

Attachments

2

Activity

Show:

Jozef Wagner March 12, 2014 at 8:53 PM

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

Alex Miller March 12, 2014 at 3:34 PM

Could you add a test to the patch?

Jozef Wagner March 12, 2014 at 10:17 AM

Patch added, following expression will now run without error

Jozef Wagner March 12, 2014 at 10:03 AM

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

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.

Completed

Details

Assignee

Reporter

Approval

Ok

Patch

Code and Test

Priority

Fix versions

Created March 11, 2014 at 9:57 PM
Updated August 29, 2014 at 4:50 PM
Resolved August 29, 2014 at 4:50 PM