'How to see parameters of a struct in Julia?

In Python the command dir on an object returns the object's properties and methods. Is there an equivalent command in Julia? I know Julia does not have methods in the sense of Python's objects, but can we see the parameters of a struct?



Solution 1:[1]

In addition to fieldnames and fieldtypes, from the other answer, the dump function can be used for introspection of both types and values, for example

julia> struct Foo{S, T<:AbstractVector{S}}
           x::T
           y::Int
       end

julia> dump(Foo)                                 # <-- dump on the type
UnionAll
  var: TypeVar
    name: Symbol S
    lb: Union{}
    ub: Any
  body: UnionAll
    var: TypeVar
      name: Symbol T
      lb: Union{}
      ub: AbstractArray{S, 1} <: Any
    body: Foo{S, T<:AbstractArray{S, 1}} <: Any
      x::T
      y::Int64

julia> dump(Foo([1, 2, 3], 1))                   # <-- dump on a value
Foo{Int64, Vector{Int64}}
  x: Array{Int64}((3,)) [1, 2, 3]
  y: Int64 1

Solution 2:[2]

@frederikekre's answer reminded me of the amazing package Eyeball.jl. It lets you interactively explore types and their instances, get docs for them, methods that operate on them (methodswith), etc. (Note however that it's at version 0.4.4, and there's still the occasional bug you might encounter.)

Here, after typing eye(fb), I pressed down arrow once to go to r: RegexMatch, and then m to get the list of methods that accept a RegexMatch.

julia> struct FooBar
         r::RegexMatch
         s::SubstitutionString
       end

julia> fb = FooBar(match(r"\d+", "test123"), s"42")
FooBar(RegexMatch("123"), s"42")

julia> eye(fb)
[f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit
     : FooBar  FooBar(RegexMatch("123"), s"42")
 >    r: RegexMatch  RegexMatch("123")
       match: SubString{String}  "123"
        string: String  "test123"
        offset: Int64  4
        ncodeunits: Int64  3
       captures: Vector{Union{Nothing, SubString{String}}} (0,) 0 Union{Nothing, SubString{String}}[]
       offset: Int64  5
       offsets: Vector{Int64} (0,) 0 Int64[]
       regex: Regex  r"\d+"
        pattern: String  "\\d+"
        compile_options: UInt32  0x040a0002
        match_options: UInt32  0x40000000
        regex: Ptr{Nothing}  Ptr{Nothing} @0x00000000024163f0
      s: SubstitutionString{String}  s"42"
       string: String  "42

Opening methodswith(`r`) ...

[f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit
 >   : Vector{Method} (12,) 96 Method[FooBar(r::RegexMatch, s::SubstitutionString) in Main at REPL[25]:2, eltype(m::RegexMatch) in Bas
      1: Method  FooBar(r::RegexMatch, s::SubstitutionString) in Main at REPL[25]:2
      2: Method  eltype(m::RegexMatch) in Base at regex.jl:262
      3: Method  getindex(m::RegexMatch, idx::Integer) in Base at regex.jl:245
      4: Method  getindex(m::RegexMatch, name::AbstractString) in Base at regex.jl:251
      5: Method  getindex(m::RegexMatch, name::Symbol) in Base at regex.jl:246
      6: Method  haskey(m::RegexMatch, idx::Integer) in Base at regex.jl:253
      7: Method  haskey(m::RegexMatch, name::AbstractString) in Base at regex.jl:258
      8: Method  haskey(m::RegexMatch, name::Symbol) in Base at regex.jl:254
      9: Method  iterate(m::RegexMatch, args...) in Base at regex.jl:260
      10: Method  keys(m::RegexMatch) in Base at regex.jl:219
      11: Method  length(m::RegexMatch) in Base at regex.jl:261
      12: Method  show(io::IO, m::RegexMatch) in Base at regex.jl:227

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fredrikekre
Solution 2 Sundar R