# Practical use for the \`only\` function?

**URL:** https://discourse.julialang.org/t/practical-use-for-the-only-function/100280
**Category:** New to Julia
**Created:** [June 13, 2023, 1:00pm UTC](https://discourse.julialang.org/t/practical-use-for-the-only-function/100280 "2023-06-13T13:00:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 13, 2023, 1:00pm UTC](https://discourse.julialang.org/t/practical-use-for-the-only-function/100280/1 "2023-06-13T13:00:37Z")

</div>

I’ve tried to search around github a bit, but [only](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.only) is a pretty common word. Can someone give me an example of a practical use for this function?

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [June 13, 2023, 1:09pm UTC](https://discourse.julialang.org/t/practical-use-for-the-only-function/100280/2 "2023-06-13T13:09:50Z")

</div>

[AbstractDifferentiation](https://github.com/JuliaDiff/AbstractDifferentiation.jl) uses `only` because derivative/grad/jacobian functions are forced to return tuples with respect to each input, but these tuples must be length one in some cases. Users might mess these cases up when they define custom derivative rules, so this can help catch them.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 13, 2023, 1:13pm UTC](https://discourse.julialang.org/t/practical-use-for-the-only-function/100280/3 "2023-06-13T13:13:47Z")

</div>

Yes, to expand on what @jacobusmmsmit said, `only` is useful when a function generally returns a collection of elements, but you know that the collection should contain just a single element in your specific case and would be invalid otherwise. `only` helps you catch the invalid states.

EDIT: You could view `only` as a shortcut, I guess:

```julia
elements = some_function()
@assert length(elements) == 1
x = elements[begin]

# or with `only`

elements = some_function()
x = only(elements)

```
