List.Chars and String.Chars
Get an overview of the List.Chars and String.Chars protocols.
We'll cover the following
Introduction
The List.Chars
protocol is used by Kernel.to_charlist
to convert a value into a list of characters (think single-quoted string).
The String.Chars
protocol is used to convert a value to a string (a binary/double-quoted string). This is the protocol used for string interpolation.
The protocols are implemented identically, except List.Chars
requires that we write a to_charlist
function, and String.Chars
requires we write to_string
.
Although we could implement a String.Chars.to_string
for our Midi
struct, it probably wouldn’t make much sense. What would we interpolate into the string?
Protocols are polymorphism
When we want to write a function that behaves differently depending on the type of its arguments, we’re looking at a polymorphic function.
Get hands-on with 1400+ tech skills courses.