Skip to content

Commit 0510512

Browse files
author
José Valim
committed
Deprecate to_binary in favor of to_string
We also rename Binary.Chars to String.Chars. As Binary.Chars was the protocol used for interpolation, it is semantically correct to have a protocol that works on strings, instead of raw binaries.
1 parent a382752 commit 0510512

38 files changed

+394
-396
lines changed

lib/eex/lib/eex/engine.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule EEx.Engine do
4343
def handle_expr(buffer, "=", expr) do
4444
quote do
4545
tmp = unquote(buffer)
46-
tmp <> to_binary(unquote(expr))
46+
tmp <> to_string(unquote(expr))
4747
end
4848
end
4949

lib/elixir/lib/binary/chars.ex

Lines changed: 0 additions & 84 deletions
This file was deleted.

lib/elixir/lib/code.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ defmodule Code do
118118
end
119119

120120
defp do_eval_string(string, binding, opts) when is_list(binding) do
121-
{ value, binding, _scope } =
122-
:elixir.eval String.to_char_list!(string), binding, opts
121+
{ value, binding, _scope } = :elixir.eval to_char_list(string), binding, opts
123122
{ value, binding }
124123
end
125124

@@ -216,10 +215,10 @@ defmodule Code do
216215
`Macro.to_string/1`, which converts a quoted form to a string/binary
217216
representation.
218217
"""
219-
def string_to_quoted(string, opts // []) do
218+
def string_to_quoted(string, opts // []) when is_list(opts) do
220219
file = Keyword.get opts, :file, "nofile"
221220
line = Keyword.get opts, :line, 1
222-
res = :elixir_translator.forms(String.to_char_list!(string), line, file, opts)
221+
res = :elixir_translator.forms(to_char_list(string), line, file, opts)
223222

224223
case res do
225224
{ :ok, forms } -> { :ok, unpack_quote(line, forms) }
@@ -235,10 +234,10 @@ defmodule Code do
235234
236235
Check `string_to_quoted/2` for options information.
237236
"""
238-
def string_to_quoted!(string, opts // []) do
237+
def string_to_quoted!(string, opts // []) when is_list(opts) do
239238
file = Keyword.get opts, :file, "nofile"
240239
line = Keyword.get opts, :line, 1
241-
res = :elixir_translator.forms!(String.to_char_list!(string), line, file, opts)
240+
res = :elixir_translator.forms!(to_char_list(string), line, file, opts)
242241
unpack_quote(line, res)
243242
end
244243

lib/elixir/lib/enum.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ defmodule Enum do
593593

594594
def join(collection, joiner) when is_binary(joiner) do
595595
Enumerable.reduce(collection, "", fn
596-
entry, "" -> to_binary(entry)
597-
entry, acc -> acc <> joiner <> to_binary(entry)
596+
entry, "" -> to_string(entry)
597+
entry, acc -> acc <> joiner <> to_string(entry)
598598
end)
599599
end
600600

@@ -653,18 +653,11 @@ defmodule Enum do
653653

654654
def map_join(collection, joiner, mapper) when is_binary(joiner) do
655655
Enumerable.reduce(collection, "", fn
656-
entry, "" -> to_binary(mapper, entry)
657-
entry, acc -> acc <> joiner <> to_binary(mapper, entry)
656+
entry, "" -> to_string(mapper, entry)
657+
entry, acc -> acc <> joiner <> to_string(mapper, entry)
658658
end)
659659
end
660660

661-
defp to_binary(mapper, entry) do
662-
case mapper.(entry) do
663-
x when is_binary(x) -> x
664-
o -> Binary.Chars.to_binary(o)
665-
end
666-
end
667-
668661
@doc """
669662
Invokes the given `fun` for each item in the `collection`
670663
while also keeping an accumulator. Returns a tuple where
@@ -1312,7 +1305,7 @@ defmodule Enum do
13121305

13131306
## Helpers
13141307

1315-
@compile { :inline, chunks_n: 5, chunks_step: 4 }
1308+
@compile { :inline, chunks_n: 5, chunks_step: 4, to_string: 2 }
13161309

13171310
defp chunks_n(acc, buffer, i, n, step) when i == n do
13181311
acc = [:lists.reverse(buffer)|acc]
@@ -1346,6 +1339,13 @@ defmodule Enum do
13461339
map_reduce(collection, 0, reducer)
13471340
end
13481341

1342+
defp to_string(mapper, entry) do
1343+
case mapper.(entry) do
1344+
x when is_binary(x) -> x
1345+
o -> String.Chars.to_string(o)
1346+
end
1347+
end
1348+
13491349
## Implementations
13501350

13511351
## all?

lib/elixir/lib/exception.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ defmodule Exception do
304304
"""
305305
def format_file_line(file, line, cwd // nil) do
306306
if file do
307-
file = to_binary(file)
307+
file = to_string(file)
308308
file = if cwd, do: Path.relative_to(file, cwd), else: Path.relative_to_cwd(file)
309309

310310
if line && line != 0 do

0 commit comments

Comments
 (0)