Skip to content

Commit 42d3781

Browse files
author
José Valim
committed
No more binding with a list
1 parent aeefd59 commit 42d3781

File tree

2 files changed

+21
-63
lines changed

2 files changed

+21
-63
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,8 +2054,9 @@ defmodule Kernel do
20542054
defp typespec(_), do: false
20552055

20562056
@doc """
2057-
Returns the binding as a keyword list where the variable name
2058-
is the key and the variable value is the value.
2057+
Returns the binding for the given context as a keyword list.
2058+
2059+
The variable name is the key and the variable value is the value.
20592060
20602061
## Examples
20612062
@@ -2066,59 +2067,18 @@ defmodule Kernel do
20662067
iex> binding()
20672068
[x: 2]
20682069
2069-
"""
2070-
defmacro binding() do
2071-
do_binding(nil, nil, __CALLER__.vars, Macro.Env.in_match?(__CALLER__))
2072-
end
2073-
2074-
@doc """
2075-
Receives a list of atoms at compilation time and returns the
2076-
binding of the given variables as a keyword list where the
2077-
variable name is the key and the variable value is the value.
2078-
2079-
In case a variable in the list does not exist in the binding,
2080-
it is not included in the returned result.
2081-
2082-
## Examples
2083-
2084-
iex> x = 1
2085-
iex> binding([:x, :y])
2086-
[x: 1]
2087-
2088-
"""
2089-
defmacro binding(list) when is_list(list) do
2090-
do_binding(list, nil, __CALLER__.vars, Macro.Env.in_match?(__CALLER__))
2091-
end
2092-
2093-
defmacro binding(context) when is_atom(context) do
2094-
do_binding(nil, context, __CALLER__.vars, Macro.Env.in_match?(__CALLER__))
2095-
end
2096-
2097-
@doc """
2098-
Receives a list of atoms at compilation time and returns the
2099-
binding of the given variables in the given context as a keyword
2100-
list where the variable name is the key and the variable value
2101-
is the value.
2102-
2103-
In case a variable in the list does not exist in the binding,
2104-
it is not included in the returned result.
2105-
2106-
## Examples
2107-
2108-
iex> var!(x, :foo) = 1
2109-
iex> binding([:x, :y])
2070+
iex> binding(:foo)
21102071
[]
2111-
iex> binding([:x, :y], :foo)
2072+
iex> var!(x, :foo) = 1
2073+
1
2074+
iex> binding(:foo)
21122075
[x: 1]
21132076
21142077
"""
2115-
defmacro binding(list, context) when is_list(list) and is_atom(context) do
2116-
do_binding(list, context, __CALLER__.vars, Macro.Env.in_match?(__CALLER__))
2117-
end
2118-
2119-
defp do_binding(list, context, vars, in_match) do
2120-
for {v, c} <- vars, c == context, list == nil or :lists.member(v, list) do
2121-
{v, wrap_binding(in_match, {v, [], c})}
2078+
defmacro binding(context \\ nil) do
2079+
in_match? = Macro.Env.in_match?(__CALLER__)
2080+
for {v, c} <- __CALLER__.vars, c == context do
2081+
{v, wrap_binding(in_match?, {v, [], c})}
21222082
end
21232083
end
21242084

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ defmodule KernelTest do
3131

3232
test "match?/2" do
3333
assert match?(_, List.first(1)) == true
34-
assert binding([:x]) == []
34+
assert binding() == []
3535

3636
a = List.first([0])
3737
assert match?(b when b > a, 1) == true
38-
assert binding([:b]) == []
38+
assert binding() == [a: 0]
3939

4040
assert match?(b when b > a, -1) == false
41-
assert binding([:b]) == []
41+
assert binding() == [a: 0]
4242
end
4343

4444
test "nil?/1" do
@@ -134,21 +134,19 @@ defmodule KernelTest do
134134
assert apply(fn x -> x * 2 end, [2]) == 4
135135
end
136136

137-
test "binding/0, binding/1 and binding/2" do
137+
test "binding/0 and binding/1" do
138138
x = 1
139-
assert binding == [x: 1]
140-
assert binding([:x, :y]) == [x: 1]
141-
assert binding([:x, :y], nil) == [x: 1]
139+
assert binding() == [x: 1]
142140

143141
x = 2
144-
assert binding == [x: 2]
142+
assert binding() == [x: 2]
145143

146144
y = 3
147-
assert binding == [x: 2, y: 3]
145+
assert binding() == [x: 2, y: 3]
148146

149-
var!(x, :foo) = 2
150-
assert binding(:foo) == [x: 2]
151-
assert binding([:x, :y], :foo) == [x: 2]
147+
var!(x, :foo) = 4
148+
assert binding() == [x: 2, y: 3]
149+
assert binding(:foo) == [x: 4]
152150
end
153151

154152
defmodule User do

0 commit comments

Comments
 (0)