In vs haskey order of parameters

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

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.

1 Like

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)

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

1 Like

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.

Thank you all for the answers.