Skip to content

Commit 0e8de11

Browse files
author
José Valim
committed
Promote @impl true in behaviour examples
Signed-off-by: José Valim <[email protected]>
1 parent f6b0855 commit 0e8de11

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

lib/elixir/lib/dynamic_supervisor.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ defmodule DynamicSupervisor do
5151
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
5252
end
5353
54+
@impl true
5455
def init(_arg) do
5556
DynamicSupervisor.init(strategy: :one_for_one)
5657
end
@@ -84,6 +85,7 @@ defmodule DynamicSupervisor do
8485
Supervisor.start_child(__MODULE__, [foo, bar, baz])
8586
end
8687
88+
@impl true
8789
def init(initial_arg) do
8890
children = [
8991
# Or the deprecated: worker(MyWorker, [initial_arg])
@@ -110,6 +112,7 @@ defmodule DynamicSupervisor do
110112
DynamicSupervisor.start_child(__MODULE__, spec)
111113
end
112114
115+
@impl true
113116
def init(initial_arg) do
114117
DynamicSupervisor.init(
115118
strategy: :one_for_one,
@@ -208,9 +211,6 @@ defmodule DynamicSupervisor do
208211
end
209212

210213
defoverridable child_spec: 1
211-
212-
@doc false
213-
def init(arg)
214214
end
215215
end
216216

lib/elixir/lib/gen_server.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ defmodule GenServer do
2323
2424
# Callbacks
2525
26+
@impl true
27+
def init(stack) do
28+
{:ok, stack}
29+
end
30+
31+
@impl true
2632
def handle_call(:pop, _from, [h | t]) do
2733
{:reply, h, t}
2834
end
2935
36+
@impl true
3037
def handle_cast({:push, item}, state) do
3138
{:noreply, [item | state]}
3239
end
@@ -151,22 +158,15 @@ defmodule GenServer do
151158
152159
# Server (callbacks)
153160
161+
@impl true
154162
def handle_call(:pop, _from, [h | t]) do
155163
{:reply, h, t}
156164
end
157165
158-
def handle_call(request, from, state) do
159-
# Call the default implementation from GenServer
160-
super(request, from, state)
161-
end
162-
166+
@impl true
163167
def handle_cast({:push, item}, state) do
164168
{:noreply, [item | state]}
165169
end
166-
167-
def handle_cast(request, state) do
168-
super(request, state)
169-
end
170170
end
171171
172172
In practice, it is common to have both server and client functions in
@@ -196,11 +196,13 @@ defmodule GenServer do
196196
GenServer.start_link(__MODULE__, %{})
197197
end
198198
199+
@impl true
199200
def init(state) do
200201
schedule_work() # Schedule work to be performed on start
201202
{:ok, state}
202203
end
203204
205+
@impl true
204206
def handle_info(:work, state) do
205207
# Do the desired work here
206208
schedule_work() # Reschedule once more
@@ -664,8 +666,8 @@ defmodule GenServer do
664666
{:ok, args}
665667
end
666668
667-
But you want to define your own implementation that converts the \
668-
arguments given to GenServer.start_link/3 to the server state
669+
You can copy the implementation above or define your own that converts \
670+
the arguments given to GenServer.start_link/3 to the server state.
669671
"""
670672

671673
:elixir_errors.warn(env.line, env.file, message)

lib/elixir/lib/supervisor.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ defmodule Supervisor do
2828
2929
## Callbacks
3030
31+
@impl true
3132
def init(stack) do
3233
{:ok, stack}
3334
end
3435
36+
@impl true
3537
def handle_call(:pop, _from, [h | t]) do
3638
{:reply, h, t}
3739
end
3840
41+
@impl true
3942
def handle_cast({:push, h}, t) do
4043
{:noreply, [h | t]}
4144
end
@@ -342,6 +345,7 @@ defmodule Supervisor do
342345
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
343346
end
344347
348+
@impl true
345349
def init(_arg) do
346350
children = [
347351
{Stack, [:hello]}
@@ -476,9 +480,6 @@ defmodule Supervisor do
476480
end
477481

478482
defoverridable child_spec: 1
479-
480-
@doc false
481-
def init(arg)
482483
end
483484
end
484485

0 commit comments

Comments
 (0)