Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Dearth of find methods #62

Open
davidagold opened this issue Sep 13, 2015 · 7 comments
Open

Dearth of find methods #62

davidagold opened this issue Sep 13, 2015 · 7 comments

Comments

@davidagold
Copy link
Contributor

Currently the only method for find that specializes on NullableArray arguments is find(X::NullableArray{Bool}). Users will probably want to be able to do things like find(x->x==Nullable(1), X), but this currently errors:

julia> X
10-element NullableArray{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> find(x->x==Nullable(1), X)
ERROR: TypeError: non-boolean (Nullable{Bool}) used in boolean context
 in find at array.jl:754

In general, the fact that comparisons of Nullables return Nullable{Bool}s is the root cause of a lot of unnecessary code reuse. For now I'll just write more NullableArray specific methods, but I wonder if there's any chance of getting a more institutional fix for this issue, e.g. language-wide defined behavior for if Nullable{Bool} ....

@nalimilan
Copy link
Member

I'd say that's indeed a much broader issue than regarding find. Actually, it's even broader than Nullable{Bool}: I don't think we can change the behavior of the latter with also changing the fact that +(::Nullable{Int}, ::Nullable{Int}) returns a Nullable{Int}.

As you say, it could make sense to get some language support so that if Nullable(true) would be equivalent to if get(Nullable(true)). That's how it works in R, where everything is nullable -- raising an error when testing for a missing value. And indeed it's probably the only way to get Nullable working everywhere without rewriting too many functions -- which is painful but doable for Base, but completely impossible when you consider all packages that you may want to use in combination with Nullable`s.

Maybe we should open an issue to discuss this in Julia Base? The trend is to use Nullables much more frequently in the API, so it's kind of the right time. A related discussion happened at JuliaLang/julia#9711, but it was more about the interaction with the short-circuit behaviour.

@nalimilan
Copy link
Member

I've just realized that an option to avoid special-casing Nullable would be for if to call convert(Bool, x). That would only be workable if this method was only defined for a very limited set of types, and in particular not for any numeric (with != 0 equivalent to true). Not sure that would be appreciated.

@johnmyleswhite
Copy link
Member

I think it's very unlikely that Julia will ever allow any form of automatic conversion to Bool.

@davidagold
Copy link
Contributor Author

@johnmyleswhite I do as well, but it's kind of unfortunate that one has to include an entire copy of a function's code just to tweak the conditionals and nothing else. This especially since Julia's multiple dispatch is otherwise so good for extending methods.

@nalimilan
Copy link
Member

You're probably right. It would be good to discuss this anyway.

@nalimilan
Copy link
Member

I've filed JuliaLang/julia#13207, please comment if my description is not accurate.

@davidagold
Copy link
Contributor Author

Thanks, Milan. I do have something to add, which I'll do shortly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants