Skip to content

Commit b77e65d

Browse files
Improve funcion signatures and guard consistency in Module (#15215)
Before function signatures showed arguments as: arg, tuple, tuples. Now they appear as: definition, definitions. Guards has been improved and standardized, along with variable names.
1 parent 37bbba9 commit b77e65d

1 file changed

Lines changed: 38 additions & 29 deletions

File tree

lib/elixir/lib/module.ex

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ defmodule Module do
680680
information. For documentation, see `c:Module.__info__/1`.
681681
'''
682682

683-
@type definition :: {atom, arity}
683+
@type definition :: {function_name :: atom, arity}
684684
@type def_kind :: :def | :defp | :defmacro | :defmacrop
685685

686686
@type create_opts :: [
@@ -1250,17 +1250,18 @@ defmodule Module do
12501250
12511251
"""
12521252
@spec defines?(module, definition) :: boolean
1253-
def defines?(module, {name, arity} = tuple)
1254-
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 do
1253+
def defines?(module, {function_name, arity} = definition)
1254+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1255+
arity <= 255 do
12551256
{set, _bag} = data_tables_for!(module, __ENV__.function, @extra_error_msg_defines?)
1256-
:ets.member(set, {:def, tuple})
1257+
:ets.member(set, {:def, definition})
12571258
end
12581259

12591260
@doc """
12601261
Checks if the module defines a function or macro of the
1261-
given `kind`.
1262+
given kind.
12621263
1263-
`kind` can be any of `:def`, `:defp`, `:defmacro`, or `:defmacrop`.
1264+
`def_kind` can be any of `:def`, `:defp`, `:defmacro`, or `:defmacrop`.
12641265
12651266
This function can only be used on modules that have not yet been compiled.
12661267
Use `Kernel.function_exported?/3` and `Kernel.macro_exported?/3` to check for
@@ -1276,12 +1277,13 @@ defmodule Module do
12761277
12771278
"""
12781279
@spec defines?(module, definition, def_kind) :: boolean
1279-
def defines?(module, {name, arity} = tuple, def_kind)
1280-
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 and
1280+
def defines?(module, {function_name, arity} = definition, def_kind)
1281+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1282+
arity <= 255 and
12811283
def_kind in [:def, :defp, :defmacro, :defmacrop] do
12821284
{set, _bag} = data_tables_for!(module, __ENV__.function, @extra_error_msg_defines?)
12831285

1284-
case :ets.lookup(set, {:def, tuple}) do
1286+
case :ets.lookup(set, {:def, definition}) do
12851287
[{_, ^def_kind, _, _, _, _}] -> true
12861288
_ -> false
12871289
end
@@ -1294,7 +1296,9 @@ defmodule Module do
12941296
"""
12951297
@doc since: "1.7.0"
12961298
@spec defines_type?(module, definition) :: boolean
1297-
def defines_type?(module, definition) when is_atom(module) do
1299+
def defines_type?(module, {function_name, arity} = definition)
1300+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1301+
arity <= 255 do
12981302
Kernel.Typespec.defines_type?(module, definition)
12991303
end
13001304

@@ -1307,7 +1311,9 @@ defmodule Module do
13071311
"""
13081312
@doc since: "1.7.0"
13091313
@spec spec_to_callback(module, definition) :: boolean
1310-
def spec_to_callback(module, definition) do
1314+
def spec_to_callback(module, {function_name, arity} = definition)
1315+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1316+
arity <= 255 do
13111317
Kernel.Typespec.spec_to_callback(module, definition)
13121318
end
13131319

@@ -1408,10 +1414,10 @@ defmodule Module do
14081414
14091415
"""
14101416
@spec definitions_in(module, def_kind) :: [definition]
1411-
def definitions_in(module, kind)
1412-
when is_atom(module) and kind in [:def, :defp, :defmacro, :defmacrop] do
1417+
def definitions_in(module, def_kind)
1418+
when is_atom(module) and def_kind in [:def, :defp, :defmacro, :defmacrop] do
14131419
{set, _} = data_tables_for!(module, __ENV__.function, @extra_error_msg_definitions_in)
1414-
:ets.select(set, [{{{:def, :"$1"}, kind, :_, :_, :_, :_}, [], [:"$1"]}])
1420+
:ets.select(set, [{{{:def, :"$1"}, def_kind, :_, :_, :_, :_}, [], [:"$1"]}])
14151421
end
14161422

14171423
@doc """
@@ -1442,16 +1448,17 @@ defmodule Module do
14421448
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
14431449
| nil
14441450
@doc since: "1.12.0"
1445-
def get_definition(module, {name, arity}, options \\ [])
1446-
when is_atom(module) and is_atom(name) and is_integer(arity) and is_list(options) do
1451+
def get_definition(module, {function_name, arity} = _definition, options \\ [])
1452+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1453+
arity <= 255 and is_list(options) do
14471454
{set, bag} = data_tables_for!(module, __ENV__.function, "")
14481455

1449-
case :ets.lookup(set, {:def, {name, arity}}) do
1456+
case :ets.lookup(set, {:def, {function_name, arity}}) do
14501457
[{_key, kind, meta, _, _, _}] ->
14511458
clauses =
14521459
if options[:skip_clauses],
14531460
do: [],
1454-
else: bag_lookup_element(bag, {:clauses, {name, arity}}, 2)
1461+
else: bag_lookup_element(bag, {:clauses, {function_name, arity}}, 2)
14551462

14561463
{:v1, kind, meta, clauses}
14571464

@@ -1468,10 +1475,11 @@ defmodule Module do
14681475
"""
14691476
@doc since: "1.12.0"
14701477
@spec delete_definition(module, definition) :: boolean()
1471-
def delete_definition(module, {name, arity})
1472-
when is_atom(module) and is_atom(name) and is_integer(arity) do
1478+
def delete_definition(module, {function_name, arity} = _definition)
1479+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1480+
arity <= 255 do
14731481
assert_not_compiled!(__ENV__.function, module, :writeable)
1474-
:elixir_def.take_definition(module, {name, arity}) != false
1482+
:elixir_def.take_definition(module, {function_name, arity}) != false
14751483
end
14761484

14771485
@doc """
@@ -1487,20 +1495,20 @@ defmodule Module do
14871495
given.
14881496
"""
14891497
@spec make_overridable(module, [definition]) :: :ok
1490-
def make_overridable(module, tuples) when is_atom(module) and is_list(tuples) do
1498+
def make_overridable(module, definitions) when is_atom(module) and is_list(definitions) do
14911499
assert_not_compiled!(__ENV__.function, module, :writeable)
14921500

14931501
func = fn
1494-
{function_name, arity} = tuple
1502+
{function_name, arity} = definition
14951503
when is_atom(function_name) and is_integer(arity) and arity >= 0 and arity <= 255 ->
1496-
case :elixir_def.take_definition(module, tuple) do
1504+
case :elixir_def.take_definition(module, definition) do
14971505
false ->
14981506
raise ArgumentError,
14991507
"cannot make function #{function_name}/#{arity} " <>
15001508
"overridable because it was not defined"
15011509

15021510
clause ->
1503-
:elixir_overridable.record_overridable(module, tuple, clause)
1511+
:elixir_overridable.record_overridable(module, definition, clause)
15041512
end
15051513

15061514
other ->
@@ -1509,7 +1517,7 @@ defmodule Module do
15091517
"{function_name :: atom, arity :: 0..255} tuple, got: #{inspect(other)}"
15101518
end
15111519

1512-
:lists.foreach(func, tuples)
1520+
:lists.foreach(func, definitions)
15131521
end
15141522

15151523
@spec make_overridable(module, module) :: :ok
@@ -1567,9 +1575,10 @@ defmodule Module do
15671575
exists or one is pending.
15681576
"""
15691577
@spec overridable?(module, definition) :: boolean
1570-
def overridable?(module, {function_name, arity} = tuple)
1571-
when is_atom(function_name) and is_integer(arity) and arity >= 0 and arity <= 255 do
1572-
:elixir_overridable.overridable_for(module, tuple) != :not_overridable
1578+
def overridable?(module, {function_name, arity} = definition)
1579+
when is_atom(module) and is_atom(function_name) and is_integer(arity) and arity >= 0 and
1580+
arity <= 255 do
1581+
:elixir_overridable.overridable_for(module, definition) != :not_overridable
15731582
end
15741583

15751584
@doc """

0 commit comments

Comments
 (0)