-
Notifications
You must be signed in to change notification settings - Fork 0
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
WIP: Add only
function
#3
Conversation
The function `only(x)` returns the one-and-only element of a collection `x`, or else throws an error.
only(x::NamedTuple{<:Any, <:Tuple{Any}}) = first(x) | ||
only(x::NamedTuple) = throw( | ||
ArgumentError("NamedTuple contains $(length(x)) elements, must contain exactly 1 element") | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure of style in Base, but this fits with other files as far as I can tell...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am tempted to say this should be a BoundsError
or a DimensionMismatch
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- yeah, I can see the case for
BoundsError
as the error type... but on the other hand the message doesn't really feel right
julia> x = [1, 2];
julia> throw(BoundsError(x, 2))
ERROR: BoundsError: attempt to access 2-element Array{Int64,1} at index [2]
-
Similarly, with
DimensionMismatch
, what is the "mismatch"? -
ArgumentError
feels better to me... but i'd certainly like to hear other opinions!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair
22585c2
to
04d1c74
Compare
You made a PR tio invenia/julia. |
only(x::Tuple) = throw( | ||
ArgumentError("Tuple contains $(length(x)) elements, must contain exactly 1 element") | ||
) | ||
only(a::AbstractArray{<:Any, 0}) = @inbounds return a[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it not possible to construct a zero length AbstractArray{<:Any, 0}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I test that? i.e. How does one create a zero-dim array of any length?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
julia> collect(1)
0-dimensional Array{Int64,0}:
1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idk how to create an empty one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know the answer to this (and Andy Ferris is the wise fellow who actually wrote this)
I will take that as a word of encouragement! |
04d1c74
to
a2c60af
Compare
a2c60af
to
e2d543a
Compare
89c1f98
to
9235f44
Compare
Thanks for reviewing -- moved to JuliaLang#33129 |
Previously, we might accidentally leave behind content in the fields that should not be there. For example: ``` julia> code_typed(() -> (TypeVar(:x),), (), optimize=false) 1-element Vector{Any}: CodeInfo( @ REPL[1]:1 within `#3' 1 ─ %1 = Main.TypeVar(:x)::Core.Compiler.PartialTypeVar(x, true, true) │ %2 = Core.tuple(%1)::Core.PartialStruct(Tuple{TypeVar}, Any[Core.Compiler.PartialTypeVar(x, true, true)]) └── return %2 ) => Tuple{TypeVar} julia> ans[1][1].rettype Core.PartialStruct(Tuple{TypeVar}, Any[Core.Compiler.PartialTypeVar(x, true, true)]) ```
The function
only(x)
returns the one-and-only element of a collectionx
, or else throws an error.