Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make falses(A) and trues(A) not change their argument #15827

Merged
merged 1 commit into from
Apr 18, 2016

Conversation

martinholters
Copy link
Member

Presently, we have:

julia> a=[false true];

julia> falses(a)
1x2 BitArray{2}:
 false  false

julia> a
1x2 BitArray{2}:
 false  false

which is undocumented and certainly surprising. This PR makes falses(A) and trues(A) not change their argument and documents this syntax.

@@ -236,7 +236,9 @@ function fill!(B::BitArray, x)
end

falses(args...) = fill!(BitArray(args...), false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also restrict the signature here. As this PR shows, too broad signatures can give unexpected results. Looks like we need two methods: falses(::Dims) and falses(::Integer...).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Updated accordingly and DRYed up.

@@ -235,8 +235,11 @@ function fill!(B::BitArray, x)
return B
end

falses(args...) = fill!(BitArray(args...), false)
trues(args...) = fill!(BitArray(args...), true)
for (f, v) in ((:falses, false), (:trues, true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't think the @eval machinery is worth it here. It makes the code much harder to read.

Also, do you know if both variants are currently tested?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't think the @eval machinery is worth it here. It makes the code much harder to read.

After making two of the cases just call the third instead of all of them calling fill!, I couldn't stop myself. But you're probably right.

Also, do you know if both variants are currently tested?

No, but even if both of them get implicitly tested somewhere by just using them, it's probably a good idea to explicitly test them as part of the tests of this PR.


falses(A)

Create a `BitArray` with all values set to `false` of the same shape as `A`.
"""
falses
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the docstrings inline while you're at it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good opportunity indeed, will do so tomorrow.

@JeffBezanson
Copy link
Member

Thanks! This is indeed a bad bug.

@martinholters martinholters force-pushed the fix_trues_falses branch 2 times, most recently from c84a48c to e4a1bbd Compare April 12, 2016 06:30
@martinholters martinholters changed the title RFC: Make falses(A) and trues(A) not change their argument Make falses(A) and trues(A) not change their argument Apr 12, 2016
falses(A)

Create a `BitArray` with all values set to `false` of the same shape as `A`.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may as well separate the docstrings for the separate signatures, I think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, yes, just tried to, but then genstdlib.jl does not include the docs for the second signature in its output. Any idea what I'm doing wrong? The REPL output with "?" looks the same as before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to manually add the first line with the signature into the rst for genstdlib to match it and populate the rest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, thanks.

Make falses(A::BitArray) and trues(A::BitArray) not change their
arguments by implementing falses(A::AbstractArray) and
trues(A::AbstractArray) and replacing the completely generic case with
more specific ones for ::Dims and ::Interger... arguments. Update
documentation accordingly and move it inline while at it.
@martinholters
Copy link
Member Author

Could be merged, no?

@tkelman tkelman merged commit a136a6e into JuliaLang:master Apr 18, 2016
@martinholters martinholters deleted the fix_trues_falses branch April 18, 2016 08:04
tkelman pushed a commit that referenced this pull request Jun 17, 2016
Make falses(A::BitArray) and trues(A::BitArray) not change their
arguments by implementing falses(A::AbstractArray) and
trues(A::AbstractArray) and replacing the completely generic case with
more specific ones for ::Dims and ::Interger... arguments. Update
documentation accordingly and move it inline while at it.

(cherry picked from commit 9523972)
ref #15827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants