Clojure

Can't type hint literal collection containing runtime values

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.10
  • Fix Version/s: None
  • Component/s: None
  • Environment:
    OS X 10.11.4
  • Patch:
    Code
  • Approval:
    Triaged

Description

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

Activity

There are no comments yet on this issue.

People

Vote (5)
Watch (3)

Dates

  • Created:
    Updated: