# Why does findfirst(==(T), ...) on a tuple of typed only constant fold for the first?

**URL:** https://discourse.julialang.org/t/why-does-findfirst-t-on-a-tuple-of-typed-only-constant-fold-for-the-first/68893
**Category:** Performance
**Created:** [September 28, 2021, 7:52pm UTC](https://discourse.julialang.org/t/why-does-findfirst-t-on-a-tuple-of-typed-only-constant-fold-for-the-first/68893 "2021-09-28T19:52:17Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [September 29, 2021, 1:32pm UTC](https://discourse.julialang.org/t/why-does-findfirst-t-on-a-tuple-of-typed-only-constant-fold-for-the-first/68893/3 "2021-09-29T13:32:52Z")

</div>

Should we define a recursive `findfirst` implementation for tuples?

```julia
julia> _findfirst(f, i, ::Tuple{}) = nothing
_findfirst (generic function with 1 method)

julia> _findfirst(f, i, x) = f(first(x)) ? i : _findfirst(f, i+1, Base.tail(x))
_findfirst (generic function with 2 methods)

julia> myfindfirst(f::F, x::Tuple) where {F} = _findfirst(f, 1, x)
myfindfirst (generic function with 1 method)

julia> position(::T) where {T} = myfindfirst(==(T), (Int, Float64, Char))
position (generic function with 1 method)

julia> @code_typed position(10)
CodeInfo(
1 ─ return 1
) => Int64

julia> @code_typed position(2.4)
CodeInfo(
1 ─ return 2
) => Int64

julia> @code_typed position('a')
CodeInfo(
1 ─ return 3
) => Int64

julia> @code_typed position(1f0)
CodeInfo(
1 ─ return nothing
) => Nothing

```

---

_[View the full topic](https://discourse.julialang.org/t/why-does-findfirst-t-on-a-tuple-of-typed-only-constant-fold-for-the-first/68893)._
