Welcome! Please see the About page for a little more info on how this works.

+3 votes
in Compiler by

Currently type hints on literals containing runtime values are lost:

`
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

`
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

`
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

1 Answer

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1929 (reported by glts)
...