Mimes In Julia: What Are They?
An overview of the mime concept, and how to apply mimes to your code in Julia.

Introduction
Julia input and output is very unique to Julia, and understanding it can certainly be difficult; especially coming from other programming languages. Fortunately, Julia has a very modern and intuitive way of producing its output, which is actually based on multiple dispatch itself. Understanding Julia’s output also involves understanding the concept of Julia’s Mimes. So what on Earth are mimes?
A mime is a Type in Julia, or rather — a MIME is a type in Julia. The MIME type has a parameterized distinction in the form of a symbol which tells dispatch what mime is being used. In Julia, something being parameterized means that the type was provided as a parameter to the inner constructor when it was created. For example, a Vector{Int64} contains the parameter Int64, the type of each element. In this case, instead of the parameter being of type Type, the parameter is of type Symbol. With a mime, the symbol is made from a small string that describes how the output should go.
Mime examples
Mimes are arguments for how an output should be handled in Julia. If you want to show something in Julia, for instance, you could optionally provide a mime as an argument in order to decide how it is shown. In this context, show() is the method that you want to bind initially, and display is a call to show that will convert show the display of your string to whatever you set it to:
julia> display("text/plain", "hello")
"hello"julia> display("hello")
"hello"But what if we wanted to be able to display any string as HTML content? Such as here:
display("text/html", "hello")
ERROR: MethodError: no method matching show(::Base.TTY, ::MIME{Symbol("text/html")}, ::String)
Closest candidates are:
show(::IO, ::AbstractString, ::Any) at /opt/julia-1.6.3/julia-1.6.3/share/julia/base/multimedia.jl:111
show(::IO, ::MIME{Symbol("text/plain")}, ::AbstractString; limit) at /opt/julia-1.6.3/julia-1.6.3/share/julia/base/strings/io.jl:196
show(::IO, ::MIME{Symbol("text/plain")}, ::Any) at /opt/juli ....This produces a MethodError. The great thing about a MethodError is it tells us exactly what we did wrong, and what we might have been looking for. You can read more about this throw and why I think it is so great in an article I wrote all about it here:
Anyway, in this particular case, we did not mean to call any of the closest candidates, and the arguments we used were what we wanted. Our only issue is that the method does not exist. Fortunately, we can take that same MethodError and use the knowledge of it to create our function.
show(::Base.TTY, ::MIME{Symbol("text/html")}, ::String)Of course, in order to write out extension to Base.show, we will need to explicitly directly import it:
import Base: showNow let’s bind a new method, and we will make a small function which puts a string into a <p> tag.
julia> function show(term::Base.TTY, m::MIME{Symbol("text/html")}, s::String)
println("<p>$s</p>")
end
show (generic function with 263 methods)Now if we were to call display, we would get the following return:
julia> display("text/html", "hello")
<p>hello</p>The IO portion of this is also important, but I think it really detracts away from the subject of mimes. IO is implicitly passed by whatever the output buffer actually is. You could do this with just about any IOBuffer, which means that this is not limited to only the terminal, but you could in fact write output to a buffer and then utilize that buffer later.
Conclusion
So that was my overview on the mime type. Really, mimes are just symbols that can be used with Julia dispatch in order to take different processes for output depending on how we want it to be displayed. The great thing about this is that, as in the example above, we can completely change the functionality of showing something quite easily with multiple dispatch.
Needless to say, I think mimes are pretty awesome, and they are also surprisingly easy to work with. Thank you for reading, and I hope this was helpful for anyone who might be confused about the topic of mimes in Julia, and what they actually are.
Happy programming!





