@@ -2054,8 +2054,9 @@ defmodule Kernel do
2054
2054
defp typespec ( _ ) , do: false
2055
2055
2056
2056
@ 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.
2059
2060
2060
2061
## Examples
2061
2062
@@ -2066,59 +2067,18 @@ defmodule Kernel do
2066
2067
iex> binding()
2067
2068
[x: 2]
2068
2069
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)
2110
2071
[]
2111
- iex> binding([:x, :y], :foo)
2072
+ iex> var!(x, :foo) = 1
2073
+ 1
2074
+ iex> binding(:foo)
2112
2075
[x: 1]
2113
2076
2114
2077
"""
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 } ) }
2122
2082
end
2123
2083
end
2124
2084
0 commit comments