Clojure

array-map fails lazily if passed an odd number of arguments

Details

  • Type: Defect Defect
  • Status: Reopened Reopened
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: Release 1.8
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:
    Vetted

Description

If called with an odd number of arguments, array-map does not throw an exception until the map is realized, when it throws the confusing ArrayIndexOutOfBoundsException.

Example, in 1.5.1 and 1.6.0-alpha3:

user=> (def m (hash-map :a 1 :b))
IllegalArgumentException No value supplied for key: :b  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)

user=> (def m (array-map :a 1 :b))
#'user/m
user=> (prn m)
ArrayIndexOutOfBoundsException 3
  clojure.lang.PersistentArrayMap$Seq.first
  (PersistentArrayMap.java:313)

Approach: Catch on construction and throw same error as hash-map.

user=> (def m (array-map :a 1 :b))
IllegalArgumentException No value supplied for key: :b  clojure.lang.PersistentArrayMap.createAsIfByAssoc (PersistentArrayMap.java:78)

Patch: 0002-CLJ-1319-Throw-on-odd-arguments-to-PersistentArrayMa.patch
Screened by: Alex Miller

Activity

Hide
Alex Miller added a comment -

PersistentArrayMap.createAsIfByAssoc could check length is even to catch this

Show
Alex Miller added a comment - PersistentArrayMap.createAsIfByAssoc could check length is even to catch this
Hide
Jason Felice added a comment -

A better error message would be nice... this is the best I could think of.

Show
Jason Felice added a comment - A better error message would be nice... this is the best I could think of.
Hide
OHTA Shogo added a comment -

I suffered from this problem recently. Anything to resolve before applying the patch?

Show
OHTA Shogo added a comment - I suffered from this problem recently. Anything to resolve before applying the patch?
Hide
Alex Miller added a comment -

The error message should match the message for hash-map:

user=> (hash-map 1 2 3)
IllegalArgumentException No value supplied for key: 3  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
user=> (array-map 1 2 3)
IllegalArgumentException Odd number of arguments when creating map  clojure.lang.PersistentArrayMap.createAsIfByAssoc (PersistentArrayMap.java:78)
Show
Alex Miller added a comment - The error message should match the message for hash-map:
user=> (hash-map 1 2 3)
IllegalArgumentException No value supplied for key: 3  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
user=> (array-map 1 2 3)
IllegalArgumentException Odd number of arguments when creating map  clojure.lang.PersistentArrayMap.createAsIfByAssoc (PersistentArrayMap.java:78)
Hide
OHTA Shogo added a comment - - edited

Let me have a try. I just updated the patch!

0002-CLJ-1319-Throw-on-odd-arguments-to-PersistentArrayMa.patch

Show
OHTA Shogo added a comment - - edited Let me have a try. I just updated the patch! 0002-CLJ-1319-Throw-on-odd-arguments-to-PersistentArrayMa.patch
Hide
Alex Miller added a comment -

Looks good.

Show
Alex Miller added a comment - Looks good.
Hide
Alex Miller added a comment -

Reopened - does not seem to have correct behavior in 1.8.0-alpha4.

user=> (array-map 1 2 3)
ArrayIndexOutOfBoundsException 3  clojure.lang.PersistentArrayMap$Seq.first (PersistentArrayMap.java:321)
user=> (def m (array-map :a 1 :b))
#'user/m
Show
Alex Miller added a comment - Reopened - does not seem to have correct behavior in 1.8.0-alpha4.
user=> (array-map 1 2 3)
ArrayIndexOutOfBoundsException 3  clojure.lang.PersistentArrayMap$Seq.first (PersistentArrayMap.java:321)
user=> (def m (array-map :a 1 :b))
#'user/m

People

Vote (1)
Watch (2)

Dates

  • Created:
    Updated: