Customized display of composite types in Julia

后端 未结 2 1649
北海茫月
北海茫月 2021-02-08 23:58

Suppose you define a new composite type in Julia and a variable of that type:

type MyType
  α::Int64
  β::Vector{Float64}
  γ::Float64

  MyType(α::Int64, β::Vec         


        
2条回答
  •  你的背包
    2021-02-09 00:35

    You should define one of the following (they will both work and have the same effect):

    function Base.show(io::IO, me::MyType)
        println(io, "MyType")
        println(io, "α:$(me.α), β:$(me.β), γ:$(me.γ)")
    end
    
    function Base.writemime(io::IO, ::MIME"text/plain", me::MyType)
        println(io, "MyType")
        println(io, "α:$(me.α), β:$(me.β), γ:$(me.γ)")
    end
    

提交回复
热议问题