Skip to content

Macro to display model struct the way Flux does #2044

Closed
@ericphanson

Description

@ericphanson

Semi-related to #2043 😄. Custom model structs use

@functor MyModel

to define Functors.children etc. It would be nice if there was also

@define_layer_show MyModel

to generate a method for Base.show, like

using Flux
function Base.show(io::IO, m::MIME"text/plain", x::MyModel)
    if get(io, :typeinfo, nothing) === nothing  # e.g. top level in REPL
      Flux._big_show(io, x)
    elseif !get(io, :compact, false)  # e.g. printed inside a Vector, but not a Matrix
      Flux._layer_show(io, x)
    else
      show(io, x)
    end
  end
end

Alternatives:

  • I guess this could also be built into @functor but I suspect it might be better to keep it separate/ opt-in so you can define your own show method if you want. Also, adding it to @functor would also presumably be breaking since folks may have custom show methods already and that would generate method overwrite warnings and possibly break precompilation.
  • This could be done with an abstact type, but I think it's nice if folks are free to have their model partipicate in their own type hierarchy. But an abstract type seems OK too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions