We're updating the issue view to help you get more done. 

Can't type hint literal collection containing runtime values

Description

Currently type hints on literals containing runtime values are lost:

1 2 3 4 5 user=> (set! *warn-on-reflection* true) true user=> (fn [x] (java.util.HashMap. ^java.util.Map {"foo" x})) Reflection warning, null:1:9 - call to java.util.HashMap ctor can't be resolved. #function[user/eval7479/fn--7480]

A workaround that currently works is to introduce a local and type hint the binding

1 2 user=> (fn [x] (let [^java.util.Map m {"foo" x}] (java.util.HashMap. m))) #function[user/eval7487/fn--7488]

The attached patch fixes this issue by tracking type hints for literal values, making the reflection go away even in the original case

1 2 3 4 user=> (set! *warn-on-reflection* true) true user=> (fn [x] (java.util.HashMap. ^java.util.Map {"foo" x})) #function[user/eval7479/fn--7480]

Approach: preserve user hints in literal collections
Patch: 0001-CLJ-1929-preserve-type-hints-in-literals.patch

Environment

OS X 10.11.4

Status

Assignee

Unassigned

Reporter

David Bürgin

Approval

Triaged

Patch

Code

Affects versions

Release 1.10

Priority

Major