summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Kraan <derek.kraan@gmail.com>2018-09-26 12:13:43 +0200
committerAndrea Leopardi <an.leopardi@gmail.com>2018-09-26 12:13:43 +0200
commiteb069dd43ba98958f4161b070d111f952b1c656c (patch)
treef7717d030eb94e4f74bf313e30d447a2b2bac8e4
parente6bd1adaf26a2d40ce6ccee3de41f20e13cc86ad (diff)
downloadelixir-eb069dd43ba98958f4161b070d111f952b1c656c.tar.gz
Change "args" to "init_arg" in GenServer-like modules (#8226)
-rw-r--r--lib/elixir/lib/dynamic_supervisor.ex17
-rw-r--r--lib/elixir/lib/gen_server.ex36
-rw-r--r--lib/elixir/lib/supervisor.ex28
-rw-r--r--lib/elixir/test/elixir/dynamic_supervisor_test.exs4
4 files changed, 43 insertions, 42 deletions
diff --git a/lib/elixir/lib/dynamic_supervisor.ex b/lib/elixir/lib/dynamic_supervisor.ex
index 4ee02bbf9..b014e1c3d 100644
--- a/lib/elixir/lib/dynamic_supervisor.ex
+++ b/lib/elixir/lib/dynamic_supervisor.ex
@@ -135,7 +135,7 @@ defmodule DynamicSupervisor do
Developers typically invoke `DynamicSupervisor.init/1` at the end of
their init callback to return the proper supervision flags.
"""
- @callback init(args :: term) :: {:ok, sup_flags()} | :ignore
+ @callback init(init_arg :: term) :: {:ok, sup_flags()} | :ignore
@typedoc "The supervisor flags returned on init"
@type sup_flags() :: %{
@@ -170,6 +170,7 @@ defmodule DynamicSupervisor do
| :ignore
| {:error, {:already_started, pid} | :max_children | term}
+ # In this struct, `args` refers to the arguments passed to init/1 (the `init_arg`).
defstruct [
:args,
:extra_arguments,
@@ -277,8 +278,8 @@ defmodule DynamicSupervisor do
"""
@doc since: "1.6.0"
@spec start_link(module, term, GenServer.options()) :: Supervisor.on_start()
- def start_link(mod, args, opts \\ []) do
- GenServer.start_link(__MODULE__, {mod, args, opts[:name]}, opts)
+ def start_link(mod, init_arg, opts \\ []) do
+ GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
end
@doc """
@@ -527,11 +528,11 @@ defmodule DynamicSupervisor do
## Callbacks
@impl true
- def init({mod, args, name}) do
+ def init({mod, init_arg, name}) do
Process.put(:"$initial_call", {:supervisor, mod, 1})
Process.flag(:trap_exit, true)
- case mod.init(args) do
+ case mod.init(init_arg) do
{:ok, flags} when is_map(flags) ->
name =
cond do
@@ -540,7 +541,7 @@ defmodule DynamicSupervisor do
is_tuple(name) -> name
end
- state = %DynamicSupervisor{mod: mod, args: args, name: name}
+ state = %DynamicSupervisor{mod: mod, args: init_arg, name: name}
case init(state, flags) do
{:ok, state} -> {:ok, state}
@@ -745,8 +746,8 @@ defmodule DynamicSupervisor do
end
@impl true
- def code_change(_, %{mod: mod, args: args} = state, _) do
- case mod.init(args) do
+ def code_change(_, %{mod: mod, args: init_arg} = state, _) do
+ case mod.init(init_arg) do
{:ok, flags} when is_map(flags) ->
case init(state, flags) do
{:ok, state} -> {:ok, state}
diff --git a/lib/elixir/lib/gen_server.ex b/lib/elixir/lib/gen_server.ex
index 3e31d1810..32566a8ec 100644
--- a/lib/elixir/lib/gen_server.ex
+++ b/lib/elixir/lib/gen_server.ex
@@ -368,7 +368,7 @@ defmodule GenServer do
Invoked when the server is started. `start_link/3` or `start/3` will
block until it returns.
- `args` is the argument term (second argument) passed to `start_link/3`.
+ `init_arg` is the argument term (second argument) passed to `start_link/3`.
Returning `{:ok, state}` will cause `start_link/3` to return
`{:ok, pid}` and the process to enter its loop.
@@ -405,7 +405,7 @@ defmodule GenServer do
`{:error, reason}` and the process to exit with reason `reason` without
entering the loop or calling `c:terminate/2`.
"""
- @callback init(args :: term) ::
+ @callback init(init_arg :: term) ::
{:ok, state}
| {:ok, state, timeout | :hibernate | {:continue, term}}
| :ignore
@@ -693,10 +693,10 @@ defmodule GenServer do
See `Supervisor`.
"""
- def child_spec(arg) do
+ def child_spec(init_arg) do
default = %{
id: __MODULE__,
- start: {__MODULE__, :start_link, [arg]}
+ start: {__MODULE__, :start_link, [init_arg]}
}
Supervisor.child_spec(default, unquote(Macro.escape(opts)))
@@ -778,8 +778,8 @@ defmodule GenServer do
We will inject a default implementation for now:
- def init(args) do
- {:ok, args}
+ def init(init_arg) do
+ {:ok, init_arg}
end
You can copy the implementation above or define your own that converts \
@@ -790,8 +790,8 @@ defmodule GenServer do
quote do
@doc false
- def init(args) do
- {:ok, args}
+ def init(init_arg) do
+ {:ok, init_arg}
end
defoverridable init: 1
@@ -805,7 +805,7 @@ defmodule GenServer do
This is often used to start the `GenServer` as part of a supervision tree.
Once the server is started, the `c:init/1` function of the given `module` is
- called with `args` as its arguments to initialize the server. To ensure a
+ called with `init_arg` as its argument to initialize the server. To ensure a
synchronized start-up procedure, this function does not return until `c:init/1`
has returned.
@@ -845,8 +845,8 @@ defmodule GenServer do
`{:error, reason}` or `:ignore`, respectively.
"""
@spec start_link(module, any, options) :: on_start
- def start_link(module, args, options \\ []) when is_atom(module) and is_list(options) do
- do_start(:link, module, args, options)
+ def start_link(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
+ do_start(:link, module, init_arg, options)
end
@doc """
@@ -855,23 +855,23 @@ defmodule GenServer do
See `start_link/3` for more information.
"""
@spec start(module, any, options) :: on_start
- def start(module, args, options \\ []) when is_atom(module) and is_list(options) do
- do_start(:nolink, module, args, options)
+ def start(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
+ do_start(:nolink, module, init_arg, options)
end
- defp do_start(link, module, args, options) do
+ defp do_start(link, module, init_arg, options) do
case Keyword.pop(options, :name) do
{nil, opts} ->
- :gen.start(:gen_server, link, module, args, opts)
+ :gen.start(:gen_server, link, module, init_arg, opts)
{atom, opts} when is_atom(atom) ->
- :gen.start(:gen_server, link, {:local, atom}, module, args, opts)
+ :gen.start(:gen_server, link, {:local, atom}, module, init_arg, opts)
{{:global, _term} = tuple, opts} ->
- :gen.start(:gen_server, link, tuple, module, args, opts)
+ :gen.start(:gen_server, link, tuple, module, init_arg, opts)
{{:via, via_module, _term} = tuple, opts} when is_atom(via_module) ->
- :gen.start(:gen_server, link, tuple, module, args, opts)
+ :gen.start(:gen_server, link, tuple, module, init_arg, opts)
{other, _} ->
raise ArgumentError, """
diff --git a/lib/elixir/lib/supervisor.ex b/lib/elixir/lib/supervisor.ex
index 5f53c4097..49250ad79 100644
--- a/lib/elixir/lib/supervisor.ex
+++ b/lib/elixir/lib/supervisor.ex
@@ -353,12 +353,12 @@ defmodule Supervisor do
# Automatically defines child_spec/1
use Supervisor
- def start_link(arg) do
- Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
+ def start_link(init_arg) do
+ Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
- def init(_arg) do
+ def init(_init_arg) do
children = [
{Stack, [:hello]}
]
@@ -475,10 +475,10 @@ defmodule Supervisor do
See `Supervisor`.
"""
- def child_spec(arg) do
+ def child_spec(init_arg) do
default = %{
id: __MODULE__,
- start: {__MODULE__, :start_link, [arg]},
+ start: {__MODULE__, :start_link, [init_arg]},
type: :supervisor
}
@@ -495,7 +495,7 @@ defmodule Supervisor do
Developers typically invoke `Supervisor.init/2` at the end of their
init callback to return the proper supervision flags.
"""
- @callback init(args :: term) ::
+ @callback init(init_arg :: term) ::
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
| :ignore
@@ -593,7 +593,7 @@ defmodule Supervisor do
## Examples
- def init(_arg) do
+ def init(_init_arg) do
children = [
{Stack, [:hello]}
]
@@ -756,10 +756,10 @@ defmodule Supervisor do
end
@doc """
- Starts a module-based supervisor process with the given `module` and `arg`.
+ Starts a module-based supervisor process with the given `module` and `init_arg`.
To start the supervisor, the `c:init/1` callback will be invoked in the given
- `module`, with `arg` as its argument. The `c:init/1` callback must return a
+ `module`, with `init_arg` as its argument. The `c:init/1` callback must return a
supervisor specification which can be created with the help of the `init/2`
function.
@@ -778,19 +778,19 @@ defmodule Supervisor do
# all to start_link(children, options).
@spec start_link(module, term) :: on_start
@spec start_link(module, term, GenServer.options()) :: on_start
- def start_link(module, arg, options \\ []) when is_list(options) do
+ def start_link(module, init_arg, options \\ []) when is_list(options) do
case Keyword.get(options, :name) do
nil ->
- :supervisor.start_link(module, arg)
+ :supervisor.start_link(module, init_arg)
atom when is_atom(atom) ->
- :supervisor.start_link({:local, atom}, module, arg)
+ :supervisor.start_link({:local, atom}, module, init_arg)
{:global, _term} = tuple ->
- :supervisor.start_link(tuple, module, arg)
+ :supervisor.start_link(tuple, module, init_arg)
{:via, via_module, _term} = tuple when is_atom(via_module) ->
- :supervisor.start_link(tuple, module, arg)
+ :supervisor.start_link(tuple, module, init_arg)
other ->
raise ArgumentError, """
diff --git a/lib/elixir/test/elixir/dynamic_supervisor_test.exs b/lib/elixir/test/elixir/dynamic_supervisor_test.exs
index bd84353cf..b0917faf4 100644
--- a/lib/elixir/test/elixir/dynamic_supervisor_test.exs
+++ b/lib/elixir/test/elixir/dynamic_supervisor_test.exs
@@ -173,9 +173,9 @@ defmodule DynamicSupervisorTest do
assert DynamicSupervisor.start_child(pid, {Task, fn -> :ok end}) == {:error, :max_children}
end
- defp fake_upgrade(pid, args) do
+ defp fake_upgrade(pid, init_arg) do
:ok = :sys.suspend(pid)
- :sys.replace_state(pid, fn state -> %{state | args: args} end)
+ :sys.replace_state(pid, fn state -> %{state | args: init_arg} end)
res = :sys.change_code(pid, :gen_server, 123, :extra)
:ok = :sys.resume(pid)
res