Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized display of composite types in Julia

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

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

  MyType(α::Int64, β::Vector{Float64}, γ::Float64) = new(α, β, γ)
end
mt = MyType(5, [1.2, 4.1, 2], 0.2)

Now if you are in REPL mode, you can simply check the value of mt by typing mt and pressing Enter:

mt
MyType(5,[1.2,4.1,2.0],0.2)

If I want to customize the way variables of MyType are displayed, I can define a function and use it like customized_display(mt):

function customized_display(me::MyType)
  println("MyType")
  println("α:$(me.α), β:$(me.β), γ:$(me.γ)")
end

customized_display(mt)
MyType
α:5, β:[1.2,4.1,2.0], γ:0.2

But using another function for displaying values of mt seems redundant. Which function do I need to extend such that by simply typing mt, the customized display is shown?

like image 870
Adham Avatar asked Jul 21 '15 10:07

Adham


People also ask

What is :: In Julia?

A return type can be specified in the function declaration using the :: operator. This converts the return value to the specified type. This function will always return an Int8 regardless of the types of x and y .

How do you determine the type of variable in Julia?

We can use the function typeof(x) to find the data type of the variable x. UInt32julia> typeof(5.) On top of these REAL and COMPLEX datatypes are also implemented.

What is mutable struct in Julia?

type and immutable are valid up to julia 0.6, mutable struct and struct are the names of the same objects in julia 0.6 and forward. mutable in mutable struct means that the fields can change - which is actually fairly rarely used so being immutable is the default. mutable struct 's are slower than struct s.

What is struct in Julia?

In Julia, the struct keyword defines a new Composite Type, based on given field names, and optionally annotated individual types. By default, structs cannot be modified once initialized (i.e. are inmutable unless explicitly specified as mutable)


2 Answers

Note: The answer by spencerlyon2 is no longer correct as of Julia 0.5 and later.

Given your type

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

If you want to customize the single-line display, add a method to Base.show like this:

function Base.show(io::IO, me::MyType)
    print(io, "MyType: α:", me.α, " β:", me.β, " γ:", me.γ)
end

An example of single-line display being used:

julia> [MyType(5, [1.2, 4.1, 2], 0.2)]
1-element Array{MyType,1}:
 MyType: α:5 β:[1.2, 4.1, 2.0] γ:0.2

By convention, this method should not include any newlines. This is so that it displays nicely when embedded in other objects, like arrays or matrices. Typically, it's preferred to output something that could be parsed and evaluated into an object equal to the one being shown, but this is not a hard rule.

If you want to customize the multi-line display, add a method to Base.show like this:

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

Note that if you do not include this method, then the single-line display will be used. The multi-line display is used at the REPL when your object is shown on its own:

julia> MyType(5, [1.2, 4.1, 2], 0.2)
MyType
α:5 β:[1.2, 4.1, 2.0] γ:0.2

By convention, the multi-line display should not print any trailing new lines.

like image 100
Fengyang Wang Avatar answered Oct 22 '22 02:10

Fengyang Wang


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
like image 37
spencerlyon2 Avatar answered Oct 22 '22 01:10

spencerlyon2