'get' should throw exception on non-Associative argument

Description

The implementation of clojure.core/get returns nil if its argument is not an associative collection.

This behavior can obscure common programmer errors such as:

Calling get on something which is neither nil nor an Associative collection is almost certainly a bug, and should be indicated by an exception.

CLJ-932 was accepted as a similar enhancement to clojure.core/contains?

Patch: 0003-CLJ-1107-Throw-exception-for-get-on-unsupported-type.patch

Approach: Throw IllegalArgumentException as final fall-through case in RT.getFrom instead of returning nil.

Also see: CLJ-969

Environment

None

Activity

Show:
Andy Fingerhut
March 26, 2014, 5:55 PM

Patch clj-1107-throw-on-unsupported-get-v4.patch dated Mar 26 2014 is identical to Stuart Sierra's patch 0003-CLJ-1107-Throw-exception-for-get-on-unsupported-type.patch, and retains his authorship. The only difference is in one line of diff context required in order to make it apply cleanly to latest master.

Rich Hickey
June 10, 2014, 4:54 PM

This would be a breaking change

Stuart Sierra
June 18, 2014, 12:59 AM

Arguably so was CLJ-932 (contains?), which did "break" some things that were already broken.

This is a more invasive change than CLJ-932, but I believe it is more likely to expose hidden bugs than to break intentional behavior.

import
October 7, 2014, 11:40 AM

Is it more idiomatic to use "({:a 1}, :a)" and a safe replacement to boot? E.g. could you mass replace "(get " with "(" in a code base, in order to find bugs? I am still learning the language, and not young anymore, and couldn't reliably remember the argument order. So, I found it easier to avoid (get) with maps anyways. Without it I can put the map first or second.

Alex Miller
November 14, 2016, 6:11 PM

Presumably this could also now be accomplished via a spec on "get".

Assignee

Stuart Sierra

Reporter

Stuart Sierra

Labels

Approval

Triaged

Patch

Code and Test

Priority

Major
Configure