eltype should only be defined for containers/collections:
help?> eltype
search: eltype valtype keytype fieldtype oftype fieldtypes supertype subtypes code_typed
eltype(type)
Determine the type of the elements generated by iterating a collection of the given
type. [...]
It’s not a generic “get a type parameter” function.
That kind of functions doesn’t (and can’t, in general) exist, because what exactly a type parameter means to the type depends on that type. It doesn’t generalize.
julia> struct MyStruct{T}
x::T
end
julia> ms=MyStruct{Union{Float64, Missing}}(1.0);
julia> typeof(ms.x)
Float64
julia> typeof(ms).parameters[1]
Union{Missing, Float64}
julia> extract_the_T(::MyStruct{T}) where T = T
extract_the_T (generic function with 1 method)
julia> extract_the_T(ms)
Union{Missing, Float64}
PS. On current master, extract_the_T2(ms) = typeof(ms).parameters[1] does constant propagate. This might not const-prop on earlier julia versions – test it in order to avoid inference failures.
I’m pretty sure that .parameters is an internal field, so not realiable. Extracting the type parameter in a dedicated function through the parameters there is IMO better, because it’s more stable & doesn’t rely on internals.
Indeed this is the most relevant question. Specifically, if the user needs to query a value or a type for something, which may be a type (eg eltype), this should be exposed via a function, and the fact that this may be encoded in a parametric type is an implementation detail.
So, in this case MyStruct was just an example I added to make what I want to do more clear, figured there probably was an easy way for this.
I want to apply this to a structure I do not actually own. Basically, there’s this DiffEq composite structure which typeof is reaaaaaaly long. To try and figure out what it actually is, I wanted to look at the parametric types on by one.
I see — this is for code exploration only. In that case, I would recommend dump, and of course reading the source code, but typeof(T).parameters is fine too.