Skip to content

Commit a1a576e

Browse files
committed
Implement as_boolean/1 type
Closes #798
1 parent 1dc4bf0 commit a1a576e

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

lib/elixir/lib/enum.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defmodule Enum do
9393
9494
"""
9595
@spec all?(t) :: boolean
96-
@spec all?(t, (element -> boolean)) :: boolean
96+
@spec all?(t, (element -> as_boolean(term))) :: boolean
9797

9898
def all?(collection, fun // fn(x) -> x end)
9999

@@ -130,7 +130,7 @@ defmodule Enum do
130130
131131
"""
132132
@spec any?(t) :: boolean
133-
@spec any?(t, (element -> boolean)) :: boolean
133+
@spec any?(t, (element -> as_boolean(term))) :: boolean
134134

135135
def any?(collection, fun // fn(x) -> x end)
136136

@@ -191,7 +191,7 @@ defmodule Enum do
191191
@doc """
192192
Counts for how many items the function returns true.
193193
"""
194-
@spec count(t, (element -> boolean)) :: non_neg_integer
194+
@spec count(t, (element -> as_boolean(term))) :: non_neg_integer
195195
def count(collection, fun) when is_list(collection) do
196196
do_count(collection, fun)
197197
end
@@ -244,7 +244,7 @@ defmodule Enum do
244244
Enum.drop_while [1,2,3,4,5], fn(x) -> x < 3 end
245245
#=> [3,4,5]
246246
"""
247-
@spec drop_while(t, (element -> boolean)) :: list
247+
@spec drop_while(t, (element -> as_boolean(term))) :: list
248248
def drop_while(collection, fun) when is_list(collection) do
249249
do_drop_while(collection, fun)
250250
end
@@ -323,7 +323,7 @@ defmodule Enum do
323323
#=> [2]
324324
325325
"""
326-
@spec filter(t, (element -> boolean)) :: list
326+
@spec filter(t, (element -> as_boolean(term))) :: list
327327
def filter(collection, fun) when is_list(collection) do
328328
lc item inlist collection, fun.(item), do: item
329329
end
@@ -346,7 +346,7 @@ defmodule Enum do
346346
#=> [4]
347347
348348
"""
349-
@spec filter_map(t, (element -> boolean), (element -> element)) :: list
349+
@spec filter_map(t, (element -> as_boolean(term)), (element -> element)) :: list
350350
def filter_map(collection, filter, mapper) when is_list(collection) do
351351
lc item inlist collection, filter.(item), do: mapper.(item)
352352
end
@@ -779,7 +779,7 @@ defmodule Enum do
779779
Enum.split_while [1,2,3,4], fn x -> x == 2 end
780780
#=> { [1], [2, 3, 4] }
781781
"""
782-
@spec split_while(t, (element -> boolean)) :: {list, list}
782+
@spec split_while(t, (element -> as_boolean(term))) :: {list, list}
783783
def split_while(collection, fun) when is_list(collection) do
784784
do_split_while(collection, fun, [])
785785
end
@@ -838,7 +838,7 @@ defmodule Enum do
838838
#=> [1, 2]
839839
840840
"""
841-
@spec take_while(t, (element -> boolean)) :: list
841+
@spec take_while(t, (element -> as_boolean(term))) :: list
842842
def take_while(collection, fun) when is_list(collection) do
843843
do_take_while(collection, fun)
844844
end

lib/elixir/lib/kernel/typespec.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ defmodule Kernel.Typespec do
479479
typespec_to_ast({:type, line, :char_list, []})
480480
end
481481

482+
defp typespec_to_ast({ :remote_type, line, [{:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]] }) do
483+
typespec_to_ast({:type, line, :as_boolean, [arg]})
484+
end
485+
486+
482487
defp typespec_to_ast({ :remote_type, line, [mod, name, args] }) do
483488
args = lc arg inlist args, do: typespec_to_ast(arg)
484489
dot = { :., [line: line], [typespec_to_ast(mod), typespec_to_ast(name)] }
@@ -624,6 +629,10 @@ defmodule Kernel.Typespec do
624629
typespec((quote do: :elixir.char_list(unquote_splicing(arguments))), vars, caller)
625630
end
626631

632+
defp typespec({:as_boolean, _meta, arguments}, vars, caller) do
633+
typespec((quote do: :elixir.as_boolean(unquote_splicing(arguments))), vars, caller)
634+
end
635+
627636
defp typespec({name, meta, arguments}, vars, caller) do
628637
arguments = lc arg inlist arguments, do: typespec(arg, vars, caller)
629638
{ :type, line(meta), name, arguments }

lib/elixir/src/elixir.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
-compile({parse_transform, elixir_transform}).
99

1010
%% Top level types
11-
-export_type([char_list/0]).
11+
-export_type([char_list/0, as_boolean/1]).
1212
-type char_list() :: string().
13+
-type as_boolean(T) :: T.
1314

1415
% OTP APPLICATION API
1516

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ defmodule Typespec.TypeTest do
335335
(quote do: @type tuple_type() :: {integer()}),
336336
(quote do: @type ftype() :: (() -> any()) | (() -> integer()) | ((integer() -> integer()))),
337337
(quote do: @type cl() :: char_list()),
338+
(quote do: @type ab() :: as_boolean(term())),
338339
(quote do: @type vaf() :: (... -> any())),
339340
(quote do: @type rng() :: 1 .. 10),
340341
]

0 commit comments

Comments
 (0)