# Concrete typeof

**URL:** <https://discourse.julialang.org/t/concrete-typeof/84881>\
**Category:** General Usage\
**Tags:** question, type\
**Created:** [July 27, 2022, 4:02pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881 "2022-07-27T16:02:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas/32/26439_2.png) [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Post date:** [July 27, 2022, 4:02pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/1 "2022-07-27T16:02:44Z")

</div>

Is there a function to get the _concrete_ (numeric) type of some object of abstract type without much information about it? Consider these two bad methods:

```julia
using LinearAlgebra
x = [[1,2],[3,4]]
typeof(dot(x,x))
eltype(eltype(x))

```

The first method requires me to compute the dot product. The second requires knowledge of how many levels of abstract types I must go through before reaching the concrete-type bedrock. To give some context, this is for an optimization package.

Edit: I thought for a second and recursion solves this very easily I think:

```julia
function eleltype(x)
    elt = eltype(x)
    elelt = eltype(eltype(x))
    elt == elelt ? elt : eleltype(elelt)
end

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [July 27, 2022, 4:56pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/2 "2022-07-27T16:56:18Z")

</div>

So I see two options. You could store your vectors into a matrix and `eltype()` will retrieve the correct type:

```julia
using LinearAlgebra
x = [[1,2],[3,4]]
y = [1 2; 3 4]

dot(x,x) == dot(y,y) #true
eltype(y) # Int64

```

Or the other option is to use recursion:

```julia
deep_eltype(x) = eltype(x) <: Number ? eltype(x) : deep_eltype(first(x))

y = [[[[1]]]]
deep_eltype(y) #Int64

```

---

<div class="post-metadata">

**Author:** ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)\
**Post date:** [July 27, 2022, 5:16pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/3 "2022-07-27T17:16:39Z")

</div>

There is `LinearAlgebra.promote_leaf_eltypes`:

```julia
julia> using LinearAlgebra

julia> x = [[1,2],[3,4]]
2-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]

julia> LinearAlgebra.promote_leaf_eltypes(x)
Int64

```

---

<div class="post-metadata">

**Author:** ![nicolas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas/32/26439_2.png) [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Post date:** [July 27, 2022, 5:17pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/4 "2022-07-27T17:17:06Z")

</div>

> [@RobertGregg](#):
>
> `deep_eltype(x) = eltype(x) <: Number ? eltype(x) : deep_eltype(first(x))`

Thanks a lot Robert. Since the goal is to accept any input from users, the second one is better. To make it work for empty arrays, I would suggest this slight modification:

```julia
deep_eltype(x) = eltype(x) <: Number ? eltype(x) : deep_eltype(eltype(x))

```

---

<div class="post-metadata">

**Author:** ![nicolas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolas/32/26439_2.png) [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Post date:** [July 27, 2022, 5:30pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/5 "2022-07-27T17:30:58Z")

</div>

Thanks a lot. I thought this might already exist but I couldn’t find it. In the help for eltype, it says “See also: keytype, typeof.” Suggesting promote\_leaf\_eltypes would be useful, although it does appear to be specific to LinearAlgebra.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 27, 2022, 5:34pm UTC](https://discourse.julialang.org/t/concrete-typeof/84881/6 "2022-07-27T17:34:39Z")

</div>

> [@nicolas](#):
>
> Suggesting promote\_leaf\_eltypes would be useful, although it does appear to be specific to LinearAlgebra.

not sure if this is public API tbh
