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:
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:
function eleltype(x)
elt = eltype(x)
elelt = eltype(eltype(x))
elt == elelt ? elt : eleltype(elelt)
end
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:
deep_eltype(x) = eltype(x) <: Number ? eltype(x) : deep_eltype(eltype(x))
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.