In vs haskey order of parameters


#1

Hello,

I was wondering if there was any reason why the signatures of some of the collection methods are “inconsistent”. For example,

in(value, collection), but haskey(dict,key), but perhaps again push!(collection,value).

Especially, in appears a bit “backwards”, even just reading the code.

Thanks,

Tom


#2

I’d say that the first two functions have sort-of-opposite meanings. You can see this more clearly if you write them in infix form. in has an infix form: value in collection. haskey doesn’t but if it did: dict haskey key, which makes sense. And for functions with ! the convention seems to be that they modify their first argument, hence why collection comes first.


#3

In addition to what you’d said, another reason is that push! also allows for multiple values to be pushed to the collection, for example: push!(arr, 1, 2, 3)


#4

For that matter, we could entirely remove haskey(dict, key) in favor of key in keys(dict).


#5

At least on master, that generates slightly less efficient code than haskey (at least for Dict, I’m not sure about if it would be even worse for other types of collections), and it might require () around it depending on what sort of expression it is in.


#6

Thank you all for the answers.