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

WIP: Add only function #3

Closed
wants to merge 6 commits into from
Closed

WIP: Add only function #3

wants to merge 6 commits into from

Conversation

nickrobinson251
Copy link

The function only(x) returns the one-and-only element of a collection x, or else throws an error.

The function `only(x)` returns the one-and-only element of a collection
`x`, or else throws an error.
base/iterators.jl Outdated Show resolved Hide resolved
base/iterators.jl Outdated Show resolved Hide resolved
only(x::NamedTuple{<:Any, <:Tuple{Any}}) = first(x)
only(x::NamedTuple) = throw(
ArgumentError("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
)
Copy link
Author

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...

Copy link
Member

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 ?

Copy link
Author

@nickrobinson251 nickrobinson251 Aug 31, 2019

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!

Copy link
Member

Choose a reason for hiding this comment

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

fair

@oxinabox
Copy link
Member

You made a PR tio invenia/julia.
You should have made it to JuliaLang/julia

only(x::Tuple) = throw(
ArgumentError("Tuple contains $(length(x)) elements, must contain exactly 1 element")
)
only(a::AbstractArray{<:Any, 0}) = @inbounds return a[]
Copy link
Member

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} ?

Copy link
Author

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?

Copy link
Member

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

Copy link
Member

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

Copy link
Author

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)

base/iterators.jl Outdated Show resolved Hide resolved
base/iterators.jl Outdated Show resolved Hide resolved
@nickrobinson251
Copy link
Author

You made a PR tio invenia/julia.
You should have made it to JuliaLang/julia

I will take that as a word of encouragement!
As you know, I made this to invenia/julia becuase this would be me first PR to julialang/julia and wanted some feedback first before opening there

@nickrobinson251
Copy link
Author

Thanks for reviewing -- moved to JuliaLang#33129

nickrobinson251 pushed a commit that referenced this pull request Mar 27, 2021
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)])
```
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.

2 participants