On Wednesday, September 21, 2016 at 2:54:15 PM UTC-4, Weicheng Zhu wrote:
> Hi all,
> I have a few simple questions to ask.
> 1) What function is invoked when I type x in the following example?

display(x), which calls show(STDOUT, MIME("text/plain"), x)  [or writemime 
in Julia ≤ 0.4]

(Under the hood, this eventually calls a function Base.showarray)

2) When I define a type which takes a matrix as a member, how to define the 
> show method to print x as shown above in julia 0.5.
> julia> type mytype
>        x::Array{Float64,2}
> end
> julia> mytype(x)
> mytype([0.923288 0.0157897; 0.439387 0.50823; … ; 0.605268 0.416877; 
> 0.223898 0.558542])

You want to define two show methods: show(io::IO, x::mytype), which calls 
show(io, x.x) and outputs everything on a single line, and show(io::IO, 
m::MIME"text/plain", x::mytype), which calls show(io, m, x.x) and outputs 
multiple pretty-printed lines.

(You can also define additional show methods, e.g. for HTML or Markdown 
output, for even nicer display in an environment like IJulia that supports 
other MIME types.)

Reply via email to