Skip to content

Commit

Permalink
Add ref and doctest for only (JuliaLang#42491)
Browse files Browse the repository at this point in the history
Co-authored-by: Fredrik Ekre <[email protected]>
  • Loading branch information
kshyatt and fredrikekre authored Oct 5, 2021
1 parent 3a2441d commit bfc35de
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1342,13 +1342,32 @@ length(s::Stateful) = length(s.itr) - s.taken
"""
only(x)
Returns the one and only element of collection `x`, and throws an `ArgumentError` if the
Return the one and only element of collection `x`, or throw an [`ArgumentError`](@ref) if the
collection has zero or multiple elements.
See also [`first`](@ref), [`last`](@ref).
!!! compat "Julia 1.4"
This method requires at least Julia 1.4.
# Examples
```jldoctest
julia> only(["a"])
"a"
julia> only("a")
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> only(())
ERROR: ArgumentError: Tuple contains 0 elements, must contain exactly 1 element
Stacktrace:
[...]
julia> only(('a', 'b'))
ERROR: ArgumentError: Tuple contains 2 elements, must contain exactly 1 element
Stacktrace:
[...]
```
"""
@propagate_inbounds function only(x)
i = iterate(x)
Expand Down

0 comments on commit bfc35de

Please sign in to comment.