diff options
author | José Valim <jose.valim@dashbit.co> | 2022-01-05 15:23:44 +0100 |
---|---|---|
committer | José Valim <jose.valim@dashbit.co> | 2022-01-05 15:23:44 +0100 |
commit | f78e43b54363de2a79d0737d488c27a024d4a0e0 (patch) | |
tree | 6444755880ff76e7d1e3d570c1ded787b0c1ec0a | |
parent | 27b53b8be584f9c0d3201b5b21ec872d55778227 (diff) | |
download | elixir-f78e43b54363de2a79d0737d488c27a024d4a0e0.tar.gz |
Reduce the amount of generated code for defdelegate
-rw-r--r-- | lib/elixir/lib/kernel.ex | 28 | ||||
-rw-r--r-- | lib/elixir/lib/kernel/utils.ex | 33 |
2 files changed, 34 insertions, 27 deletions
diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 47c58565a..abf4140ff 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -5559,33 +5559,11 @@ defmodule Kernel do end quote bind_quoted: [funs: funs, opts: opts] do - target = - Keyword.get(opts, :to) || raise ArgumentError, "expected to: to be given as argument" - - as = Keyword.get(opts, :as) - - if target == __MODULE__ and is_nil(as) do - raise ArgumentError, - "defdelegate function is calling itself, which will lead to an infinite loop. You should either change the value of the :to option or specify the :as option" - end - - if is_list(funs) do - IO.warn( - "passing a list to Kernel.defdelegate/2 is deprecated, please define each delegate separately", - Macro.Env.stacktrace(__ENV__) - ) - end - - if Keyword.has_key?(opts, :append_first) do - IO.warn( - "Kernel.defdelegate/2 :append_first option is deprecated", - Macro.Env.stacktrace(__ENV__) - ) - end + target = Kernel.Utils.defdelegate_all(funs, opts, __ENV__) + # TODO: Remove List.wrap when multiple funs are no longer supported for fun <- List.wrap(funs) do - {name, args, as, as_args} = Kernel.Utils.defdelegate(fun, opts) - + {name, args, as, as_args} = Kernel.Utils.defdelegate_each(fun, opts) @doc delegate_to: {target, as, :erlang.length(as_args)} # Build the call AST by hand so it doesn't get a diff --git a/lib/elixir/lib/kernel/utils.ex b/lib/elixir/lib/kernel/utils.ex index b57f65dd8..7811828e9 100644 --- a/lib/elixir/lib/kernel/utils.ex +++ b/lib/elixir/lib/kernel/utils.ex @@ -22,9 +22,38 @@ defmodule Kernel.Utils do defp destructure_nil(count), do: [nil | destructure_nil(count - 1)] @doc """ - Callback for defdelegate. + Callback for defdelegate entry point. """ - def defdelegate(fun, opts) when is_list(opts) do + def defdelegate_all(funs, opts, env) do + to = Keyword.get(opts, :to) || raise ArgumentError, "expected to: to be given as argument" + as = Keyword.get(opts, :as) + + if to == env.module and is_nil(as) do + raise ArgumentError, + "defdelegate function is calling itself, which will lead to an infinite loop. You should either change the value of the :to option or specify the :as option" + end + + if is_list(funs) do + IO.warn( + "passing a list to Kernel.defdelegate/2 is deprecated, please define each delegate separately", + Macro.Env.stacktrace(env) + ) + end + + if Keyword.has_key?(opts, :append_first) do + IO.warn( + "Kernel.defdelegate/2 :append_first option is deprecated", + Macro.Env.stacktrace(env) + ) + end + + to + end + + @doc """ + Callback for each function in defdelegate. + """ + def defdelegate_each(fun, opts) when is_list(opts) do # TODO: Remove on v2.0 append_first? = Keyword.get(opts, :append_first, false) |