avatarEmma Boudreau

Summary

The provided content discusses the concept of MIME types in Julia, explaining how they are used to control the output format of data in the Julia programming language through multiple dispatch.

Abstract

Julia's MIME types are a fundamental aspect of its unique input and output system, which can be challenging to grasp for those accustomed to other programming languages. MIME types in Julia are represented as parameterized types with a symbol that indicates the desired output format. This allows developers to specify how data should be displayed, such as plain text or HTML. The article illustrates this with examples, including how to handle a MethodError when a MIME type method does not exist and how to create a new method to display a string as HTML content within a <p> tag. The author emphasizes the flexibility and ease of working with MIME types in Julia, thanks to its modern and intuitive multiple dispatch feature, and concludes by expressing enthusiasm for the power and simplicity of MIME types in enhancing Julia's output capabilities.

Opinions

  • The author views Julia's MethodError as a helpful tool, as it provides clear guidance on what went wrong and how to correct it.
  • MIME types are considered to be an intuitive and powerful feature of Julia, enabling developers to easily manipulate output formats.
  • The author is of the opinion that understanding MIME types is crucial for mastering Julia's output system.
  • The article conveys that working with MIME types is surprisingly straightforward due to Julia's multiple dispatch functionality.
  • The author expresses a positive attitude towards the use of MIME types in Julia, suggesting they contribute to the language's overall elegance and efficiency.

Mimes In Julia: What Are They?

An overview of the mime concept, and how to apply mimes to your code in Julia.

(image by un-mimo on pixabay)

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: show

Now 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!

Julia
Programming
Software Development
Coding
Engineering
Recommended from ReadMedium