Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Julia: get struct parameters

Say I have a struct defined as ::GridStruct{::Integer, ::Integer, ::Number}, for example:

Grid = GridStruct(DomainSize, Cells, <irrelevant details>...)
println(typeof(Grid))

yields

GridStruct{3, Int64, Float64}

how can I "extract" (read/get) the inner types/values of (3, Int64, Float64) ?
I would want to be able to read this data for various uses, such as pre-determinind the dimensions (3 in this case) and the types for dispatch (Int64 and Float65 combination).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use some sneaky generic programming:

julia> struct A{T, U, V} end

julia> typeparams(::Type{A{T, U, V}}) where {T,U,V} = (T, U, V)
typeparams (generic function with 1 method)

julia> typeparams(A{3, Int64, Float64})
(3, Int64, Float64)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading