mutable struct FactorF{V,C,T} # variables, cardinality, array type
vals::T
end
and I have an instance of Factor where T happens to be Array{Float64,3}, what is the proper way to access the Float64 ? Here is an example of an approach I found:
import Base: eltype
eltype(::FactorF{V,C,T}) where {V,C,T} = T.parameters[1]
A = FactorF{(1,2,3), (3,3,3), Array{Float64,3}}(rand(3,3,3))
eltype(A) |> println # prints Float64
Is this the proper way to do this?
Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread. (Original message )(More Info)
Thanks @liuyxpp. The definition you propose depends on information stored in a field rather than in the type itself. So I presume that the performance won’t be as good.
@abraunst proposed this other definition in slack:
eltype(::FactorF{V,C,<:AbstractArray{T}}) where {V,C,T} = T
which only depends on info stored in the type. I think I’m gonna go with this one.
I am not sure the performance. But it is not as flexible as eltype(field) approach as the eltype is achieved in a recursive way. Thus if T in your definition is another AbstractArray, then the approach proposed above will only return this AbstractArray type rather than its element type.
Not necessarily. The compiler often propagates constants in such cases, so it won’t even use the value itself.
Also, eltype can be called on type itself, so you can define
eltype(::FactorF{V,C,T}) where {V,C,T} = eltype(T)
T.parameters[1] is certainly not a good choice, because e.g for static arrays the first parameter is size, not the element type.