core.typed

Incorrect type inferred for key lookups from bounded polymorphic types

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

When a polymorphic type x has an upper bound of U, and U is an HMap or a record, I'd expect key lookups from x have the same type as key lookups from U. Currently they are inferred as Any.

(t/def-alias U (HMap :mandatory {:foo Number}))
(t/ann foo (All [[x :< U]] [x -> Number]))
(defn foo [x]
  (:foo x))

Type Error (...) Type mismatch:

Expected: 	java.lang.Number

Actual: 	Any
in: (:foo x)

Explicitly upcasting x to U with ann-form fixes the inferred type.

(t/def-alias U (HMap :mandatory {:foo Number}))
(t/ann foo (All [[x :< U]] [x -> Number]))
(defn foo [x]
  (:foo (t/ann-form x U)))

Both HMap and record bounds exhibit the same problem.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: