[julia-users] Re: Help: Printing method in julia
OK, I see. Thank you! On Wednesday, September 21, 2016 at 6:17:30 PM UTC-5, Steven G. Johnson wrote: > > > > On Wednesday, September 21, 2016 at 4:24:10 PM UTC-4, Weicheng Zhu wrote: >> >> Hi Dr. Johnson, >> Thank you very much for your help! Not I understand how it works. >> This problem has confused me a long time since I started learning julia. >> >> So if I want a `mytype` object to be printed in the pretty-printed lines >> by default, I have to define the display method, right? >> > > No. Only override show, not display. And in Julia 0.4 the 3-argument > show is called writemime, not show. >
[julia-users] Re: Help: Printing method in julia
On Wednesday, September 21, 2016 at 4:24:10 PM UTC-4, Weicheng Zhu wrote: > > Hi Dr. Johnson, > Thank you very much for your help! Not I understand how it works. > This problem has confused me a long time since I started learning julia. > > So if I want a `mytype` object to be printed in the pretty-printed lines > by default, I have to define the display method, right? > No. Only override show, not display. And in Julia 0.4 the 3-argument show is called writemime, not show.
[julia-users] Re: Help: Printing method in julia
OK, I see. It seems that if I define the two show method directly in the REPL, it doesn't work. But it works when I wrap them into a module:) On Wednesday, September 21, 2016 at 3:24:10 PM UTC-5, Weicheng Zhu wrote: > > Hi Dr. Johnson, > Thank you very much for your help! Not I understand how it works. > This problem has confused me a long time since I started learning julia. > > So if I want a `mytype` object to be printed in the pretty-printed lines > by default, I have to define the display method, right? > I guess my definition of the display method is not quite correct, although > it works in REPL. > > >> import Base.show >> type mytype >> x::Array{Float64,2} >> end >> function show(io::IO, x::mytype) >> show(io, x.x) >> end >> function show(io::IO, m::MIME"text/plain", x::mytype) >> show(io, m, x.x) >> end >> show(STDOUT, MIME("text/plain"), mytype(x)) >> function Base.display(x::mytype) >> println("mytype object:") >> show(STDOUT, MIME("text/plain"), x) >> end >> julia> x = rand(5,2) >> julia> mytype(x) >> mytype object: >> 5×2 Array{Float64,2}: >> 0.05127 0.908138 >> 0.527729 0.835109 >> 0.657212 0.275374 >> 0.119597 0.659259 >> 0.94996 0.36432 > > > > On Wednesday, September 21, 2016 at 2:27:18 PM UTC-5, Steven G. Johnson > wrote: >> >> >> >> 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.) >> >
[julia-users] Re: Help: Printing method in julia
Hi Dr. Johnson, Thank you very much for your help! Not I understand how it works. This problem has confused me a long time since I started learning julia. So if I want a `mytype` object to be printed in the pretty-printed lines by default, I have to define the display method, right? I guess my definition of the display method is not quite correct, although it works in REPL. > import Base.show > type mytype > x::Array{Float64,2} > end > function show(io::IO, x::mytype) > show(io, x.x) > end > function show(io::IO, m::MIME"text/plain", x::mytype) > show(io, m, x.x) > end > show(STDOUT, MIME("text/plain"), mytype(x)) > function Base.display(x::mytype) > println("mytype object:") > show(STDOUT, MIME("text/plain"), x) > end > julia> x = rand(5,2) > julia> mytype(x) > mytype object: > 5×2 Array{Float64,2}: > 0.05127 0.908138 > 0.527729 0.835109 > 0.657212 0.275374 > 0.119597 0.659259 > 0.94996 0.36432 On Wednesday, September 21, 2016 at 2:27:18 PM UTC-5, Steven G. Johnson wrote: > > > > 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.) >
[julia-users] Re: Help: Printing method in julia
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.)