<< Back to previous view

[CLJ-932] contains? should throw exception on non-keyed collections Created: 17/Feb/12  Updated: 18/Aug/12  Resolved: 18/Aug/12

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: None
Fix Version/s: Release 1.5

Type: Enhancement Priority: Minor
Reporter: Stuart Sierra Assignee: Stuart Sierra
Resolution: Completed Votes: 1
Labels: None

Attachments: Text File CLJ-932-0001.patch    
Patch: Code and Test
Approval: Ok

 Description   

The contains? function, given a collection which is not an associative (Map, Set, String, array), returns false instead of throwing an exception.

This is a subject of confusion when people call contains? on sequential collections like lists, and on associative collections which do not implement the Associative interface.

Other predicates, such as even?, throw an exception when passed arguments of an invalid type.



 Comments   
Comment by Stuart Sierra [ 17/Feb/12 3:30 PM ]

Patch adds fix, adds some tests, and removes tests reflecting the old behavior.

Comment by Aaron Bedra [ 15/Aug/12 12:31 PM ]

This seems to be working properly, but what about vectors?

(contains? [1 2 3] 3)
;-> false
Comment by Andy Fingerhut [ 15/Aug/12 12:40 PM ]

The doc string for contains? covers the vector and Java array case explicitly. I'm not saying that this behavior shouldn't change, but at least it is well documented what it currently does in these cases.

Comment by Aaron Bedra [ 15/Aug/12 2:03 PM ]

Agreed. I just want to make sure that we are still ok with this functionality given that things are changing. Are there others (Stuart) that want to chime in here and make the intentions clear? If this is good then I would consider this screened and ready.

Comment by Stuart Sierra [ 15/Aug/12 2:40 PM ]

Vector is Associative, so supporting contains? is valid even if it does not do what people might expect:

user=> (contains? [:a :b :c] 2)
true
user=> (contains? [:a :b :c] 7)
false

All I'm trying to change here is have contains? throw an exception if the argument is not Associative. The current behavior (returning false) was hiding a bug in my code.

I do not consider this a breaking change. I believe the docstring of contains? leaves room for this interpretation, but Rich will have the final say.

Comment by Aaron Bedra [ 15/Aug/12 2:42 PM ]

Perfect. I just wanted to make sure that this was intended.

Generated at Fri Apr 18 18:46:36 CDT 2014 using JIRA 4.4#649-r158309.