Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect the attributes of a Struct/Mutable Struct in Julia

Tags:

julia

I have a struct defined as follows:

julia> struct test
           test1::Int64
           test2::Float64
       end

I want to be able to see the names of the attributes in the struct as well as the type. What is the most straight forward way to do this? I know I can do:

julia> t = test(1,1.0)
test(1, 1.0)

julia> fieldnames(typeof(t))
(:test1, :test2)

but I would like to see the attribute name and type together.

like image 223
logankilpatrick Avatar asked Oct 30 '25 10:10

logankilpatrick


1 Answers

Use fieldtypes(typeof(t))

julia> DataFrame(name=[fieldnames(typeof(t))...], type=[fieldtypes(typeof(t))...])
2×2 DataFrame
│ Row │ name   │ type     │
│     │ Symbol │ DataType │
├─────┼────────┼──────────┤
│ 1   │ test1  │ Int64    │
│ 2   │ test2  │ Float64  │

Regarding the other answer note that dump outputs always the entire data structure which is not good for fields having complex types. Try doing dump on a struct that has a Dict field to find out my point (or just try in the console) dump(Dict()).

like image 132
Przemyslaw Szufel Avatar answered Nov 01 '25 13:11

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!