Skip to content

Commit 879499b

Browse files
author
José Valim
committed
Deprecate (fun(...) -> ...) spec style
We are deprecating this style because it reads ambiguous as a function that receives another function as argument.
1 parent e265771 commit 879499b

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

lib/elixir/lib/kernel/typespec.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,16 @@ defmodule Kernel.Typespec do
5050
5151
Any anonymous function:
5252
53-
(fun(...) -> any)
54-
or
5553
((...) -> any)
5654
or
5755
(... -> any)
5856
5957
Anonymous function with arity of zero:
6058
61-
(fun() -> type)
62-
or
6359
(() -> type)
6460
6561
Anonymous function with some arity:
6662
67-
(fun(type, type) -> type)
68-
or
6963
((type, type) -> type)
7064
or
7165
(type, type -> type)
@@ -475,12 +469,12 @@ defmodule Kernel.Typespec do
475469

476470
defp typespec_to_ast({ :var, line, var }) do
477471
var =
478-
case atom_to_binary(var) do
479-
<<"_", c :: [binary, size(1)], rest :: binary>> ->
480-
binary_to_atom("_#{String.downcase(c)}#{rest}")
481-
<<c :: [binary, size(1)], rest :: binary>> ->
482-
binary_to_atom("#{String.downcase(c)}#{rest}")
483-
end
472+
case atom_to_binary(var) do
473+
<<"_", c :: [binary, size(1)], rest :: binary>> ->
474+
binary_to_atom("_#{String.downcase(c)}#{rest}")
475+
<<c :: [binary, size(1)], rest :: binary>> ->
476+
binary_to_atom("#{String.downcase(c)}#{rest}")
477+
end
484478
{ var, line, nil }
485479
end
486480

@@ -569,6 +563,8 @@ defmodule Kernel.Typespec do
569563

570564
# Handle funs
571565
defp typespec({:->, meta, [{[{:fun, _, arguments}], cmeta, return}]}, vars, caller) when is_list(arguments) do
566+
IO.warn "(fun(...) -> ...) in typespecs is deprecated, please drop fun() and " <>
567+
"write (... -> ...) instead. #{Exception.format_stacktrace(caller.stacktrace)}"
572568
typespec({:->, meta, [{arguments, cmeta, return}]}, vars, caller)
573569
end
574570

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,13 @@ defmodule Typespec.TypeTest do
166166
assert {:mytype, {:type, _, :fun, []}, []} = spec
167167
end
168168

169-
test "@type with a fun with arguments and return type" do
170-
{spec1, spec2} = test_module do
171-
t1 = @type mytype :: (integer, integer -> integer)
172-
t2 = @type mytype2 :: (fun(integer, integer) -> integer)
173-
{t1, t2}
169+
test "@type with a fun with multiple arguments and return type" do
170+
t = test_module do
171+
@type mytype :: (integer, integer -> integer)
174172
end
175173
assert {:mytype, {:type, _, :fun, [{:type, _, :product,
176174
[{:type, _, :integer, []}, {:type, _, :integer, []}]},
177-
{:type, _, :integer, []}]}, []} = spec1
178-
assert {:mytype2, {:type, _, :fun, [{:type, _, :product,
179-
[{:type, _, :integer, []}, {:type, _, :integer, []}]},
180-
{:type, _, :integer, []}]}, []} = spec2
175+
{:type, _, :integer, []}]}, []} = t
181176
end
182177

183178
test "@type with a fun with no arguments and return type" do

0 commit comments

Comments
 (0)