Question on composite types

Is there a method that returns

Float64

if given the composite type

Point{Float64}

?
is

You can get the parameters of a concrete type with:

julia> Vector{Int}.parameters
svec(Int64, 1)

If you want to start from an instance, then a simple function can be used to extract it:

julia> struct Point{T}
       a::T
       end

julia> p=Point{Int}(42)
Point{Int64}(42)

julia> pointparam(::Point{T}) where T = T
pointparam (generic function with 1 method)

julia> pointparam(p)
Int64

3 Likes

This is what eltype is usually used for. It doesn’t come for free for custom structs, so if it is your struct you need to do

Base.eltype(::Point{T}) where {T} = T

which is the same function as the previous post but you are sticking to the common usage here.

If you are using someone else’s package (or base Julia types), eltype should already be defined.

For iterables, yes, but that may not be the case here.

eltype has a catch-all method that falls back to Any, so in a sense it is always defined. However, it is only required to be correctly implemented if the relevant parts of the iterator interface apply.

2 Likes