/
chars.ex
57 lines (47 loc) · 1.28 KB
/
chars.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Kernel, except: [to_string: 1]
defprotocol String.Chars do
@moduledoc ~S"""
The `String.Chars` protocol is responsible for
converting a structure to a Binary (only if applicable).
The only function required to be implemented is
`to_string` which does the conversion.
The `to_string` function automatically imported
by Kernel invokes this protocol. String
interpolation also invokes `to_string` in its
arguments. For example, `"foo#{bar}"` is the same
as `"foo" <> to_string(bar)`.
"""
def to_string(thing)
end
defimpl String.Chars, for: Atom do
def to_string(nil) do
""
end
def to_string(atom) do
Atom.to_string(atom)
end
end
defimpl String.Chars, for: BitString do
def to_string(thing) when is_binary(thing) do
thing
end
def to_string(thing) do
raise Protocol.UndefinedError,
protocol: @protocol,
value: thing,
description: "cannot convert a bitstring to a string"
end
end
defimpl String.Chars, for: List do
def to_string(char_list), do: List.to_string(char_list)
end
defimpl String.Chars, for: Integer do
def to_string(thing) do
Integer.to_string(thing)
end
end
defimpl String.Chars, for: Float do
def to_string(thing) do
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
end
end