Skip to content

Commit d238d50

Browse files
author
José Valim
committed
Deprecate Code.eval in favor of Code.eval_string, closes #953
1 parent 4bde3c8 commit d238d50

File tree

9 files changed

+30
-18
lines changed

9 files changed

+30
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [URI] Downcase host and scheme and URIs
3333

3434
* deprecations
35+
* [Code] `Code.eval` is deprecated in favor of `Code.eval_string`
3536
* [ExUnit] `assert left inlist right` is deprecated in favor of `assert left in right`
3637
* [IO] `IO.getb` is deprecated in favor of `IO.getn`
3738
* [List] `List.member?/2` is deprecated in favor of `Enum.member?/2`

lib/elixir/lib/code.ex

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ defmodule Code do
5757
:code.del_path(Path.expand to_char_list(path))
5858
end
5959

60+
@doc false
61+
def eval(string, binding // [], opts // []) do
62+
IO.write "[WARNING] Code.eval is deprecated, please use Code.eval_string instead\n#{Exception.format_stacktrace}"
63+
eval_string(string, binding, opts)
64+
end
65+
6066
@doc """
6167
Evalutes the contents given by string. The second argument is the
6268
binding (which should be a keyword) followed by a keyword list of
@@ -84,24 +90,24 @@ defmodule Code do
8490
8591
## Examples
8692
87-
iex> Code.eval("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
93+
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
8894
{ 3, [ {:a, 1}, {:b, 2} ] }
8995
9096
For convenience, you can my pass `__ENV__` as argument and
9197
all imports, requires and aliases will be automatically carried
9298
over:
9399
94-
iex> Code.eval("a + b", [a: 1, b: 2], __ENV__)
100+
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
95101
{ 3, [ {:a, 1}, {:b, 2} ] }
96102
97103
"""
98-
def eval(string, binding // [], opts // [])
104+
def eval_string(string, binding // [], opts // [])
99105

100-
def eval(string, binding, Macro.Env[] = env) do
101-
eval(string, binding, env.to_keywords)
106+
def eval_string(string, binding, Macro.Env[] = env) do
107+
eval_string(string, binding, env.to_keywords)
102108
end
103109

104-
def eval(string, binding, opts) do
110+
def eval_string(string, binding, opts) do
105111
{ value, binding, _scope } =
106112
:elixir.eval :unicode.characters_to_list(string), binding, opts
107113
{ value, binding }
@@ -111,7 +117,7 @@ defmodule Code do
111117
Evalutes the quoted contents.
112118
113119
This function accepts a list of environment options.
114-
Check `Code.eval` for more information.
120+
Check `Code.eval_string` for more information.
115121
116122
## Examples
117123
@@ -153,6 +159,11 @@ defmodule Code do
153159
* `:existing_atoms_only` - When true, raises an error
154160
when non-existing atoms are found by the tokenizer.
155161
162+
## Macro.to_binary/1
163+
164+
The opposite of converting a string to its AST is
165+
`Macro.to_binary`, which converts a AST to a binary
166+
representation.
156167
"""
157168
def string_to_ast(string, opts // []) do
158169
file = Keyword.get opts, :file, "nofile"

lib/elixir/lib/kernel/cli.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ defmodule Kernel.CLI do
224224
end
225225

226226
defp process_command({:eval, expr}, _config) when is_binary(expr) do
227-
Code.eval(expr, [])
227+
Code.eval_string(expr, [])
228228
:ok
229229
end
230230

lib/elixir/lib/module.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule Module do
3131
Evalutes the quotes contents in the given module context.
3232
3333
A list of environment options can also be given as argument.
34-
Check `Code.eval` for more information.
34+
Check `Code.eval_string` for more information.
3535
3636
Raises an error if the module was already compiled.
3737

lib/elixir/test/elixir/code_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ defmodule CodeTest do
1414

1515
Code.eval_quoted contents, [], file: "sample.ex", line: 13
1616

17-
test :eval do
18-
assert Code.eval("1 + 2") == { 3, [] }
19-
assert { 3, _ } = Code.eval("a + b", [a: 1, b: 2], __ENV__.location)
17+
test :eval_string do
18+
assert Code.eval_string("1 + 2") == { 3, [] }
19+
assert { 3, _ } = Code.eval_string("a + b", [a: 1, b: 2], __ENV__.location)
2020
end
2121

2222
test :eval_with_scope do
23-
assert Code.eval("one", [], delegate_locals_to: __MODULE__) == { 1, [] }
23+
assert Code.eval_string("one", [], delegate_locals_to: __MODULE__) == { 1, [] }
2424
end
2525

2626
test :eval_with_requires do
27-
assert Code.eval("Kernel.if true, do: :ok", [], requires: [Z, Kernel]) == { :ok, [] }
27+
assert Code.eval_string("Kernel.if true, do: :ok", [], requires: [Z, Kernel]) == { :ok, [] }
2828
end
2929

3030
test :eval_quoted do

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Kernel.CLI.OptionParsingTest do
1919
test :path do
2020
root = fixture_path("../../..") |> to_char_list
2121
list = elixir('-pa "#{root}/*" -pz "#{root}/lib/*" -e "IO.inspect :code.get_path"')
22-
{ path, _ } = Code.eval list, []
22+
{ path, _ } = Code.eval_string list, []
2323

2424
# pa
2525
assert Path.expand('ebin', root) in path

lib/elixir/test/elixir/system_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule SystemTest do
3939

4040
test :argv do
4141
list = elixir('-e "IO.inspect System.argv" -- -o opt arg1 arg2 --long-opt 10')
42-
{ args, _ } = Code.eval list, []
42+
{ args, _ } = Code.eval_string list, []
4343
assert args == ["-o", "opt", "arg1", "arg2", "--long-opt", "10"]
4444
end
4545

lib/mix/lib/mix/deps/lock.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Mix.Deps.Lock do
1515
def read() do
1616
case File.read(lockfile) do
1717
{ :ok, info } ->
18-
{ value, _binding } = Code.eval(info)
18+
{ value, _binding } = Code.eval_string(info)
1919
value || []
2020
{ :error, _ } ->
2121
[]

lib/mix/lib/mix/tasks/run.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule Mix.Tasks.Run do
4444
end
4545

4646
Mix.Task.run "app.start", args
47-
if head != [], do: Code.eval Enum.join(head, " ")
47+
if head != [], do: Code.eval_string Enum.join(head, " ")
4848
if opts[:no_halt], do: :timer.sleep(:infinity)
4949
end
5050

0 commit comments

Comments
 (0)