Browse files

Change function type specification syntax (WIP)

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
  • Loading branch information...
1 parent f1b81fd commit 0db1f7279b680d529371f6b213151c21562bb7a9 @yrashk yrashk committed with josevalim Nov 29, 2012
View
2 lib/elixir/lib/behaviour.ex
@@ -60,7 +60,7 @@ defmodule Behaviour do
end
quote do
- @callback unquote(name)(unquote_splicing(args)), do: unquote(return)
+ @callback unquote(name)(unquote_splicing(args)) :: unquote(return)
Behaviour.store_docs __MODULE__, unquote(__CALLER__.line), unquote(name), unquote(arity)
end
end
View
30 lib/elixir/lib/dict.ex
@@ -55,7 +55,7 @@ defmodule Dict do
Dict.keys d #=> [:a,:b]
"""
- @spec keys(t), do: [key]
+ @spec keys(t) :: [key]
def keys(dict) do
elem(dict, 0).keys(dict)
end
@@ -69,7 +69,7 @@ defmodule Dict do
Dict.values d #=> [1,2]
"""
- @spec values(t), do: [value]
+ @spec values(t) :: [value]
def values(dict) do
elem(dict, 0).values(dict)
end
@@ -83,7 +83,7 @@ defmodule Dict do
Dict.size d #=> 2
"""
- @spec size(t), do: non_neg_integer
+ @spec size(t) :: non_neg_integer
def size(dict) do
elem(dict, 0).size(dict)
end
@@ -98,7 +98,7 @@ defmodule Dict do
Dict.has_key?(d, :b) #=> false
"""
- @spec has_key?(t, key), do: boolean
+ @spec has_key?(t, key) :: boolean
def has_key?(dict, key) do
elem(dict, 0).has_key?(dict, key)
end
@@ -115,8 +115,8 @@ defmodule Dict do
Dict.get d, :b, 3 #=> 3
"""
- @spec get(t, key), do: value | nil
- @spec get(t, key, value), do: value
+ @spec get(t, key) :: value | nil
+ @spec get(t, key, value) :: value
def get(dict, key, default // nil) do
elem(dict, 0).get(dict, key, default)
end
@@ -132,7 +132,7 @@ defmodule Dict do
Dict.get d, :b #=> raises KeyError[key: :b]
"""
- @spec get!(t, key), do: value | no_return
+ @spec get!(t, key) :: value | no_return
def get!(dict, key) do
elem(dict, 0).get!(dict, key)
end
@@ -148,7 +148,7 @@ defmodule Dict do
#=> [a: 3, b: 2]
"""
- @spec put(t, key, value), do: t
+ @spec put(t, key, value) :: t
def put(dict, key, val) do
elem(dict, 0).put(dict, key, val)
end
@@ -166,7 +166,7 @@ defmodule Dict do
Dict.delete d, :a #=> [b: 2]
"""
- @spec delete(t, key), do: t
+ @spec delete(t, key) :: t
def delete(dict, key) do
elem(dict, 0).delete(dict, key)
end
@@ -185,7 +185,7 @@ defmodule Dict do
#=> [a: 3, b: 2, d: 4]
"""
- @spec merge(t, t), do: t
+ @spec merge(t, t) :: t
def merge(dict1, dict2) do
merge(dict1, dict2, fn(_k, _v1, v2) -> v2 end)
end
@@ -204,7 +204,7 @@ defmodule Dict do
#=> [a: 4, b: 2, d: 4]
"""
- @spec merge(t, t, fun(key, value, value, do: value)), do: t
+ @spec merge(t, t, fun(key, value, value) :: value) :: t
def merge(dict1, dict2, fun) do
elem(dict1, 0).merge(dict1, dict2, fun)
end
@@ -220,7 +220,7 @@ defmodule Dict do
#=> [a: -1, b: 2]
"""
- @spec update(t, key, fun(value, do: value)), do: t
+ @spec update(t, key, fun(value) :: value) :: t
def update(dict, key, fun) do
elem(dict, 0).update(dict, key, fun)
end
@@ -237,15 +237,15 @@ defmodule Dict do
#=> [a: 1, b: 2, c: 3]
"""
- @spec update(t, key, value, fun(value, do: value)), do: t
+ @spec update(t, key, value, fun(value) :: value) :: t
def update(dict, key, initial, fun) do
elem(dict, 0).update(dict, key, initial, fun)
end
@doc """
Returns an empty dict of the same type as `dict`.
"""
- @spec empty(t), do: t
+ @spec empty(t) :: t
def empty(dict) do
elem(dict, 0).empty(dict)
end
@@ -254,7 +254,7 @@ defmodule Dict do
Returns a list of key-value pairs stored in `dict`.
No particular order is enforced.
"""
- @spec to_list(t), do: list
+ @spec to_list(t) :: list
def to_list(dict) do
elem(dict, 0).to_list(dict)
end
View
72 lib/elixir/lib/enum.ex
@@ -91,8 +91,8 @@ defmodule Enum do
Enum.all? [1,nil,3] #=> false
"""
- @spec all?(t), do: boolean
- @spec all?(t, fun(element, do: boolean)), do: boolean
+ @spec all?(t) :: boolean
+ @spec all?(t, fun(element) :: boolean) :: boolean
def all?(collection, fun // fn(x) -> x end)
@@ -128,8 +128,8 @@ defmodule Enum do
Enum.any? [false,true,false] #=> true
"""
- @spec any?(t), do: boolean
- @spec any?(t, fun(element, do: boolean)), do: boolean
+ @spec any?(t) :: boolean
+ @spec any?(t, fun(element) :: boolean) :: boolean
def any?(collection, fun // fn(x) -> x end)
@@ -160,7 +160,7 @@ defmodule Enum do
Enum.at! [2,4,6], 4 #=> raises Enum.OutOfBoundsError
"""
- @spec at!(t, non_neg_integer), do: element | no_return
+ @spec at!(t, non_neg_integer) :: element | no_return
def at!(collection, n) when is_list(collection) and n >= 0 do
do_at!(collection, n)
end
@@ -182,15 +182,15 @@ defmodule Enum do
Enum.count [1,2,3] #=> 3
"""
- @spec count(t), do: non_neg_integer
+ @spec count(t) :: non_neg_integer
def count(collection) do
I.count(collection)
end
@doc """
Counts for how many items the function returns true.
"""
- @spec count(t, fun(element, do: boolean)), do: non_neg_integer
+ @spec count(t, fun(element) :: boolean) :: non_neg_integer
def count(collection, fun) when is_list(collection) do
do_count(collection, fun)
end
@@ -215,7 +215,7 @@ defmodule Enum do
Enum.drop [1,2,3], 0 #=> [1,2,3]
"""
- @spec drop(t, integer), do: t
+ @spec drop(t, integer) :: t
def drop(collection, count) when is_list(collection) and count >= 0 do
do_drop(collection, count)
end
@@ -243,7 +243,7 @@ defmodule Enum do
Enum.drop_while [1,2,3,4,5], fn(x) -> x < 3 end
#=> [3,4,5]
"""
- @spec drop_while(t, fun(element, do: boolean)), do: t
+ @spec drop_while(t, fun(element) :: boolean) :: t
def drop_while(collection, fun) when is_list(collection) do
do_drop_while(collection, fun)
end
@@ -266,7 +266,7 @@ defmodule Enum do
Enum.each ['some', 'example'], fn(x) -> IO.puts x end
"""
- @spec each(t, fun(element, do: any)), do: :ok
+ @spec each(t, fun(element) :: any) :: :ok
def each(collection, fun) when is_list(collection) do
:lists.foreach(fun, collection)
:ok
@@ -291,7 +291,7 @@ defmodule Enum do
Enum.empty? [1,2,3] #=> false
"""
- @spec empty?(t), do: boolean
+ @spec empty?(t) :: boolean
def empty?(collection) when is_list(collection) do
collection == []
end
@@ -313,7 +313,7 @@ defmodule Enum do
#=> [2]
"""
- @spec filter(t, fun(element, do: boolean)), do: t
+ @spec filter(t, fun(element) :: boolean) :: t
def filter(collection, fun) when is_list(collection) do
lc item inlist collection, fun.(item), do: item
end
@@ -336,7 +336,7 @@ defmodule Enum do
#=> [4]
"""
- @spec filter_map(t, fun(element, do: boolean), fun(element, do: element)), do: t
+ @spec filter_map(t, fun(element) :: boolean, fun(element) :: element) :: t
def filter_map(collection, filter, mapper) when is_list(collection) do
lc item inlist collection, filter.(item), do: mapper.(item)
end
@@ -366,8 +366,8 @@ defmodule Enum do
#=> 3
"""
- @spec find(t, fun(element, do: any)), do: element | :nil
- @spec find(t, any, fun(element, do: any)), do: element | :nil
+ @spec find(t, fun(element) :: any) :: element | :nil
+ @spec find(t, any, fun(element) :: any) :: element | :nil
def find(collection, ifnone // nil, fun)
@@ -397,8 +397,8 @@ defmodule Enum do
#=> true
"""
- @spec find_value(t, fun(element, do: any)), do: any | :nil
- @spec find_value(t, any, fun(element, do: any)), do: any | :nil
+ @spec find_value(t, fun(element) :: any) :: any | :nil
+ @spec find_value(t, any, fun(element) :: any) :: any | :nil
def find_value(collection, ifnone // nil, fun)
@@ -430,7 +430,7 @@ defmodule Enum do
#=> 2
"""
- @spec find_index(t, fun(element, do: any)), do: non_neg_integer | :nil
+ @spec find_index(t, fun(element) :: any) :: non_neg_integer | :nil
def find_index(collection, fun) when is_list(collection) do
do_find_index(collection, 0, fun)
end
@@ -453,7 +453,7 @@ defmodule Enum do
Enum.first [1,2,3] #=> 1
"""
- @spec first(t), do: :nil | element
+ @spec first(t) :: :nil | element
def first([]), do: nil
def first([h|_]), do: h
@@ -482,8 +482,8 @@ defmodule Enum do
Enum.join([1,2,3], ' = ') #=> '1 = 2 = 3'
"""
- @spec join(t), do: String.t
- @spec join(t, String.t | char_list), do: String.t | char_list
+ @spec join(t) :: String.t
+ @spec join(t, String.t | char_list) :: String.t | char_list
def join(collection, joiner // "")
def join(collection, joiner) when is_list(joiner) do
@@ -517,7 +517,7 @@ defmodule Enum do
#=> [a: -1, b: -2]
"""
- @spec map(t, fun(element, do: any)), do: t
+ @spec map(t, fun(element) :: any) :: t
def map(collection, fun) when is_list(collection) do
lc item inlist collection, do: fun.(item)
end
@@ -548,8 +548,8 @@ defmodule Enum do
Enum.map_join([1,2,3], ' = ', &1 * 2) #=> '2 = 4 = 6'
"""
- @spec map_join(t, fun(element, do: any)), do: String.t
- @spec map_join(t, String.t | char_list, fun(element, do: any)), do: String.t | char_list
+ @spec map_join(t, fun(element) :: any) :: String.t
+ @spec map_join(t, String.t | char_list, fun(element) :: any) :: String.t | char_list
def map_join(collection, joiner // "", mapper)
def map_join(collection, joiner, mapper) when is_list(joiner) do
@@ -584,7 +584,7 @@ defmodule Enum do
#=> { [2, 4, 6], 6 }
"""
- @spec map_reduce(t, any, fun(element, any, do: any)), do: any
+ @spec map_reduce(t, any, fun(element, any) :: any) :: any
def map_reduce(collection, acc, f) when is_list(collection) do
:lists.mapfoldl(f, acc, collection)
end
@@ -609,7 +609,7 @@ defmodule Enum do
#=> { [2], [1,3] }
"""
- @spec partition(t, fun(element, do: any)), do: {t, t}
+ @spec partition(t, fun(element) :: any) :: {t, t}
def partition(collection, fun) when is_list(collection) do
do_partition(collection, fun, [], [])
end
@@ -634,7 +634,7 @@ defmodule Enum do
#=> 6
"""
- @spec reduce(t, any, fun(element, any, do: any)), do: any
+ @spec reduce(t, any, fun(element, any) :: any) :: any
def reduce(collection, acc, fun) when is_list(collection) do
:lists.foldl(fun, acc, collection)
end
@@ -657,7 +657,7 @@ defmodule Enum do
#=> [3, 2, 1]
"""
- @spec reverse(t), do: t
+ @spec reverse(t) :: t
def reverse(collection) when is_list(collection) do
:lists.reverse(collection)
end
@@ -670,7 +670,7 @@ defmodule Enum do
end
@doc false
- @spec qsort(t), do: t
+ @spec qsort(t) :: t
def qsort(collection) when is_list(collection) do
IO.write "[WARNING] Enum.qsort is deprecated, please use Enum.sort instead\n#{Exception.formatted_stacktrace}"
do_list_qsort(collection, [])
@@ -694,7 +694,7 @@ defmodule Enum do
Enum.sort [3,2,1] #=> [1,2,3]
"""
- @spec sort(t), do: t
+ @spec sort(t) :: t
def sort(collection) when is_list(collection) do
:lists.sort(collection)
end
@@ -716,7 +716,7 @@ defmodule Enum do
Enum.sort [3,2,1], &1 > &2 #=> [1,2,3]
"""
- @spec sort(t, fun(element, element, do: boolean)), do: t
+ @spec sort(t, fun(element, element) :: boolean) :: t
def sort(collection, fun) when is_list(collection) do
:lists.sort(fun, collection)
end
@@ -749,7 +749,7 @@ defmodule Enum do
Enum.split [1,2,3], -5 #=> { [], [1,2,3] }
"""
- @spec split(t, integer), do: {t, t}
+ @spec split(t, integer) :: {t, t}
def split(collection, count) when is_list(collection) and count >= 0 do
do_split(collection, count, [])
end
@@ -777,7 +777,7 @@ defmodule Enum do
Enum.split_while [1,2,3,4], fn x -> x == 2 end
#=> { [1], [2, 3, 4] }
"""
- @spec split_while(t, fun(element, do: boolean)), do: {t, t}
+ @spec split_while(t, fun(element) :: boolean) :: {t, t}
def split_while(collection, fun) when is_list(collection) do
do_split_while(collection, fun, [])
end
@@ -802,7 +802,7 @@ defmodule Enum do
Enum.take [1,2,3], 0 #=> []
"""
- @spec take(t, integer), do: t
+ @spec take(t, integer) :: t
def take(collection, count) when is_list(collection) and count >= 0 do
do_take(collection, count)
end
@@ -831,7 +831,7 @@ defmodule Enum do
#=> [1, 2]
"""
- @spec take_while(t, fun(element, do: boolean)), do: t
+ @spec take_while(t, fun(element) :: boolean) :: t
def take_while(collection, fun) when is_list(collection) do
do_take_while(collection, fun)
end
@@ -851,7 +851,7 @@ defmodule Enum do
dictated by the first enum. In case the second list is shorter,
values are filled with nil.
"""
- @spec zip(t, t), do: [{any, any}]
+ @spec zip(t, t) :: [{any, any}]
def zip(coll1, coll2) when is_list(coll1) do
do_zip(coll1, iterator(coll2))
end
View
146 lib/elixir/lib/kernel.ex
@@ -299,7 +299,7 @@ defmodule Kernel do
abs(-3.33) #=> 3.33
abs(-3) #=> 3
"""
- @spec abs(number), do: number
+ @spec abs(number) :: number
def abs(number) do
:erlang.abs(number)
end
@@ -313,7 +313,7 @@ defmodule Kernel do
#=> 4
"""
- @spec apply(fun, list), do: term
+ @spec apply(fun(...) :: any, list) :: term
def apply(fun, args) do
:erlang.apply(fun, args)
end
@@ -327,7 +327,7 @@ defmodule Kernel do
#=> [3,2,1]
"""
- @spec apply(atom, atom, list), do: term
+ @spec apply(atom, atom, list) :: term
def apply(module, fun, args) do
:erlang.apply(module, fun, args)
end
@@ -344,7 +344,7 @@ defmodule Kernel do
atom_to_binary(:elixir, :utf8) #=> "elixir"
"""
- @spec atom_to_binary(atom, :utf8 | :unicode | :latin1), do: binary
+ @spec atom_to_binary(atom, :utf8 | :unicode | :latin1) :: binary
def atom_to_binary(atom, encoding) do
:erlang.atom_to_binary(atom, encoding)
end
@@ -357,7 +357,7 @@ defmodule Kernel do
atom_to_list(:elixir) #=> 'elixir'
"""
- @spec atom_to_list(atom), do: list
+ @spec atom_to_list(atom) :: list
def atom_to_list(atom) do
:erlang.atom_to_list(atom)
end
@@ -380,7 +380,7 @@ defmodule Kernel do
binary_part "foo", 3, -1 #=> 1
"""
- @spec binary_part(binary, pos_integer, integer), do: binary
+ @spec binary_part(binary, pos_integer, integer) :: binary
def binary_part(binary, start, length) do
:erlang.binary_part(binary, start, length)
end
@@ -396,23 +396,23 @@ defmodule Kernel do
binary_to_atom("elixir", :utf8) #=> :elixir
"""
- @spec binary_to_atom(binary, :utf8 | :unicode | :latin1), do: atom
+ @spec binary_to_atom(binary, :utf8 | :unicode | :latin1) :: atom
def binary_to_atom(binary, encoding) do
:erlang.binary_to_atom(binary, encoding)
end
@doc """
Works like `binary_to_atom/2`, but the atom must already exist.
"""
- @spec binary_to_existing_atom(binary, :utf8 | :unicode | :latin1), do: atom
+ @spec binary_to_existing_atom(binary, :utf8 | :unicode | :latin1) :: atom
def binary_to_existing_atom(binary, encoding) do
:erlang.binary_to_existing_atom(binary, encoding)
end
@doc """
Returns a list of integers which correspond to the bytes of `binary`.
"""
- @spec binary_to_list(binary), do: list
+ @spec binary_to_list(binary) :: list
def binary_to_list(binary) do
:erlang.binary_to_list(binary)
end
@@ -422,7 +422,7 @@ defmodule Kernel do
from position `start` to position `stop` in `binary`. Positions in the binary
are numbered starting from 1.
"""
- @spec binary_to_list(binary, pos_integer, pos_integer), do: list
+ @spec binary_to_list(binary, pos_integer, pos_integer) :: list
def binary_to_list(binary, start, stop) do
:erlang.binary_to_list(binary, start, stop)
end
@@ -437,7 +437,7 @@ defmodule Kernel do
binary_to_term(term_to_binary("foo")) #=> "foo"
"""
- @spec binary_to_term(binary), do: term
+ @spec binary_to_term(binary) :: term
def binary_to_term(binary) do
:erlang.binary_to_term(binary)
end
@@ -461,7 +461,7 @@ defmodule Kernel do
binary_to_term(term_to_binary("foo"), [:safe])
"""
- @spec binary_to_term(binary, [] | [:safe]), do: term
+ @spec binary_to_term(binary, [] | [:safe]) :: term
def binary_to_term(binary, options) do
:erlang.binary_to_term(binary, options)
end
@@ -477,7 +477,7 @@ defmodule Kernel do
bit_size(<<1,2,3>>) #=> 24
"""
- @spec bit_size(bitstring), do: non_neg_integer
+ @spec bit_size(bitstring) :: non_neg_integer
def bit_size(bitstring) do
:erlang.bit_size(bitstring)
end
@@ -487,7 +487,7 @@ defmodule Kernel do
number of bits in the binary is not divisible by 8, the last element of the list will
be a bitstring containing the remaining bits (1 up to 7 bits).
"""
- @spec bitstring_to_list(bitstring), do: list
+ @spec bitstring_to_list(bitstring) :: list
def bitstring_to_list(bitstring) do
:erlang.bitstring_to_list(bitstring)
end
@@ -505,7 +505,7 @@ defmodule Kernel do
byte_size(<<1,2,3>>) #=> 3
"""
- @spec byte_size(bitstring), do: non_neg_integer
+ @spec byte_size(bitstring) :: non_neg_integer
def byte_size(bitstring) do
:erlang.byte_size(bitstring)
end
@@ -521,15 +521,15 @@ defmodule Kernel do
exit(:seems_bad)
"""
- @spec exit(term), do: no_return
+ @spec exit(term) :: no_return
def exit(reason) do
:erlang.exit(reason)
end
@doc """
Converts the given number to a float. Allowed in guard clauses.
"""
- @spec float(number), do: float
+ @spec float(number) :: float
def float(number) do
:erlang.float(number)
end
@@ -543,15 +543,15 @@ defmodule Kernel do
#=> '7.00000000000000000000e+00'
"""
- @spec float_to_list(number), do: char_list
+ @spec float_to_list(number) :: char_list
def float_to_list(number) do
:erlang.float_to_list(number)
end
@doc """
Returns the head of a list, raises badarg if the list is empty.
"""
- @spec hd(list), do: term
+ @spec hd(list) :: term
def hd(list) do
:erlang.hd(list)
end
@@ -565,7 +565,7 @@ defmodule Kernel do
#=> '7'
"""
- @spec integer_to_list(integer), do: char_list
+ @spec integer_to_list(integer) :: char_list
def integer_to_list(number) do
:erlang.integer_to_list(number)
end
@@ -580,7 +580,7 @@ defmodule Kernel do
#=> "3FF"
"""
- @spec integer_to_list(integer, pos_integer), do: char_list
+ @spec integer_to_list(integer, pos_integer) :: char_list
def integer_to_list(number, base) do
:erlang.integer_to_list(number, base)
end
@@ -594,7 +594,7 @@ defmodule Kernel do
#=> 4
"""
- @spec iolist_size(iolist), do: non_neg_integer
+ @spec iolist_size(iolist) :: non_neg_integer
def iolist_size(item) do
:erlang.iolist_size(item)
end
@@ -612,7 +612,7 @@ defmodule Kernel do
#=> <<1,2,3,1,2,3,4,5,4,6>>
"""
- @spec iolist_to_binary(iolist), do: binary
+ @spec iolist_to_binary(iolist) :: binary
def iolist_to_binary(item) do
:erlang.iolist_to_binary(item)
end
@@ -621,7 +621,7 @@ defmodule Kernel do
Returns true if the local node is alive; that is,
if the node can be part of a distributed system.
"""
- @spec is_alive, do: boolean
+ @spec is_alive :: boolean
def is_alive do
:erlang.is_alive
end
@@ -631,7 +631,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_atom(term), do: boolean
+ @spec is_atom(term) :: boolean
def is_atom(term) do
:erlang.is_atom(term)
end
@@ -643,7 +643,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_binary(term), do: boolean
+ @spec is_binary(term) :: boolean
def is_binary(term) do
:erlang.is_binary(term)
end
@@ -653,7 +653,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_bitstring(term), do: boolean
+ @spec is_bitstring(term) :: boolean
def is_bitstring(term) do
:erlang.is_bitstring(term)
end
@@ -664,7 +664,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_boolean(term), do: boolean
+ @spec is_boolean(term) :: boolean
def is_boolean(term) do
:erlang.is_boolean(term)
end
@@ -674,7 +674,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_float(term), do: boolean
+ @spec is_float(term) :: boolean
def is_float(term) do
:erlang.is_float(term)
end
@@ -684,7 +684,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_function(term), do: boolean
+ @spec is_function(term) :: boolean
def is_function(term) do
:erlang.is_function(term)
end
@@ -695,7 +695,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_function(term, non_neg_integer), do: boolean
+ @spec is_function(term, non_neg_integer) :: boolean
def is_function(term, arity) do
:erlang.is_function(term, arity)
end
@@ -705,7 +705,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_integer(term), do: boolean
+ @spec is_integer(term) :: boolean
def is_integer(term) do
:erlang.is_integer(term)
end
@@ -715,7 +715,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_list(term), do: boolean
+ @spec is_list(term) :: boolean
def is_list(term) do
:erlang.is_list(term)
end
@@ -726,7 +726,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_number(term), do: boolean
+ @spec is_number(term) :: boolean
def is_number(term) do
:erlang.is_number(term)
end
@@ -736,7 +736,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_pid(term), do: boolean
+ @spec is_pid(term) :: boolean
def is_pid(term) do
:erlang.is_pid(term)
end
@@ -746,7 +746,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_port(term), do: boolean
+ @spec is_port(term) :: boolean
def is_port(term) do
:erlang.is_port(term)
end
@@ -756,7 +756,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_reference(term), do: boolean
+ @spec is_reference(term) :: boolean
def is_reference(term) do
:erlang.is_reference(term)
end
@@ -766,7 +766,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec is_tuple(term), do: boolean
+ @spec is_tuple(term) :: boolean
def is_tuple(term) do
:erlang.is_tuple(term)
end
@@ -780,7 +780,7 @@ defmodule Kernel do
length([1,2,3,4,5,6,7,8,9]) #=> 9
"""
- @spec length(list), do: non_neg_integer
+ @spec length(list) :: non_neg_integer
def length(list) do
:erlang.length(list)
end
@@ -792,7 +792,7 @@ defmodule Kernel do
list_to_atom('elixir') #=> :elixir
"""
- @spec list_to_atom(char_list), do: atom
+ @spec list_to_atom(char_list) :: atom
def list_to_atom(char_list) do
:erlang.list_to_atom(char_list)
end
@@ -804,7 +804,7 @@ defmodule Kernel do
list_to_binary('Elixir') #=> "Elixir"
"""
- @spec list_to_binary(iolist), do: binary
+ @spec list_to_binary(iolist) :: binary
def list_to_binary(char_list) do
:erlang.list_to_binary(char_list)
end
@@ -823,7 +823,7 @@ defmodule Kernel do
#=> <<1,2,3,1,2,3,4,5,4,6,7|4>>
"""
- @spec list_to_bitstring(maybe_improper_list(char | binary | iolist | bitstring, binary | bitstring | [])), do: bitstring
+ @spec list_to_bitstring(maybe_improper_list(char | binary | iolist | bitstring, binary | bitstring | [])) :: bitstring
def list_to_bitstring(bitstring_list) do
:erlang.list_to_bitstring(bitstring_list)
end
@@ -832,7 +832,7 @@ defmodule Kernel do
Returns the atom whose text representation is `char_list`, but only if there already
exists such atom.
"""
- @spec list_to_existing_atom(char_list), do: atom
+ @spec list_to_existing_atom(char_list) :: atom
def list_to_existing_atom(char_list) do
:erlang.list_to_existing_atom(char_list)
end
@@ -844,7 +844,7 @@ defmodule Kernel do
list_to_float('2.2017764e+0') #=> 2.2017764
"""
- @spec list_to_float(char_list), do: float
+ @spec list_to_float(char_list) :: float
def list_to_float(char_list) do
:erlang.list_to_float(char_list)
end
@@ -856,7 +856,7 @@ defmodule Kernel do
list_to_integer('123') #=> 123
"""
- @spec list_to_integer(char_list), do: integer
+ @spec list_to_integer(char_list) :: integer
def list_to_integer(char_list) do
:erlang.list_to_integer(char_list)
end
@@ -868,7 +868,7 @@ defmodule Kernel do
list_to_integer('3FF', 16) #=> 1023
"""
- @spec list_to_integer(char_list, non_neg_integer), do: integer
+ @spec list_to_integer(char_list, non_neg_integer) :: integer
def list_to_integer(char_list, base) do
:erlang.list_to_integer(char_list, base)
end
@@ -886,7 +886,7 @@ defmodule Kernel do
## Examples
list_to_pid('<0.41>') #=> <0.4.1>
"""
- @spec list_to_pid(char_list), do: pid
+ @spec list_to_pid(char_list) :: pid
def list_to_pid(char_list) do
:erlang.list_to_pid(char_list)
end
@@ -898,7 +898,7 @@ defmodule Kernel do
list_to_tuple([:share, [:elixir, 163]]). #=> {:share, [:elixir, 163]}
"""
- @spec list_to_tuple(list), do: tuple
+ @spec list_to_tuple(list) :: tuple
def list_to_tuple(list) do
:erlang.list_to_tuple(list)
end
@@ -915,7 +915,7 @@ defmodule Kernel do
#=> #Ref<0.0.0.135>
"""
- @spec make_ref(), do: reference
+ @spec make_ref() :: reference
def make_ref() do
:erlang.make_ref()
end
@@ -930,7 +930,7 @@ defmodule Kernel do
max(1, 2) #=> 2
"""
- @spec max(term, term), do: term
+ @spec max(term, term) :: term
def max(first, second) do
:erlang.max(first, second)
end
@@ -945,7 +945,7 @@ defmodule Kernel do
min(1, 2) #=> 1
"""
- @spec min(term, term), do: term
+ @spec min(term, term) :: term
def min(first, second) do
:erlang.min(first, second)
end
@@ -956,7 +956,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec node(), do: node
+ @spec node() :: node
def node do
:erlang.node
end
@@ -968,7 +968,7 @@ defmodule Kernel do
Allowed in guard tests.
"""
- @spec node(pid|reference|port), do: node
+ @spec node(pid|reference|port) :: node
def node(arg) do
:erlang.node(arg)
end
@@ -985,7 +985,7 @@ defmodule Kernel do
It should not be used in application programs.
"""
- @spec pid_to_list(pid), do: list
+ @spec pid_to_list(pid) :: list
def pid_to_list(pid) do
:erlang.pid_to_list(pid)
end
@@ -999,7 +999,7 @@ defmodule Kernel do
round(5.5) #=> 6
"""
- @spec round(number), do: integer
+ @spec round(number) :: integer
def round(number) do
:erlang.round(number)
end
@@ -1008,7 +1008,7 @@ defmodule Kernel do
Returns the pid (process identifier) of the calling process.
Allowed in guard clauses.
"""
- @spec self(), do: pid
+ @spec self() :: pid
def self() do
:erlang.self()
end
@@ -1017,7 +1017,7 @@ defmodule Kernel do
Returns the size of the given argument, which must be a tuple
or a binary. If possible, please use tuple_size or binary_size.
"""
- @spec size(tuple|binary), do: non_neg_integer
+ @spec size(tuple|binary) :: non_neg_integer
def size(arg) do
:erlang.size(arg)
end
@@ -1038,7 +1038,7 @@ defmodule Kernel do
end
"""
- @spec spawn(fun), do: pid
+ @spec spawn(fun() :: any) :: pid
def spawn(fun) do
:erlang.spawn(fun)
end
@@ -1055,7 +1055,7 @@ defmodule Kernel do
spawn(SomeModule, :function, [1,2,3])
"""
- @spec spawn(module, atom, list), do: pid
+ @spec spawn(module, atom, list) :: pid
def spawn(module, fun, args) do
:erlang.spawn(module, fun, args)
end
@@ -1077,7 +1077,7 @@ defmodule Kernel do
end
"""
- @spec spawn_link(fun), do: pid
+ @spec spawn_link(fun() :: any) :: pid
def spawn_link(fun) do
:erlang.spawn_link(fun)
end
@@ -1094,7 +1094,7 @@ defmodule Kernel do
spawn_link(SomeModule, :function, [1,2,3])
"""
- @spec spawn_link(module, atom, list), do: pid
+ @spec spawn_link(module, atom, list) :: pid
def spawn_link(module, fun, args) do
:erlang.spawn_link(module, fun, args)
end
@@ -1107,7 +1107,7 @@ defmodule Kernel do
to a file in an efficient way, or sending an Erlang term to some type
of communications channel not supported by distributed :
"""
- @spec term_to_binary(term), do: binary
+ @spec term_to_binary(term) :: binary
def term_to_binary(term) do
:erlang.term_to_binary(term)
end
@@ -1121,23 +1121,23 @@ defmodule Kernel do
for more details
"""
- @spec term_to_binary(term, list({:compressed, 0..9}|{:minor_version, 0}|{:minor_version, 1})), do: binary
+ @spec term_to_binary(term, list({:compressed, 0..9}|{:minor_version, 0}|{:minor_version, 1})) :: binary
def term_to_binary(term, opts) do
:erlang.term_to_binary(term, opts)
end
@doc """
A non-local return from a function. Check try/2 for more information.
"""
- @spec throw(term), do: no_return
+ @spec throw(term) :: no_return
def throw(term) do
:erlang.throw(term)
end
@doc """
Returns the tail of a list. Raises ArgumentError if the list is empty.
"""
- @spec tl(maybe_improper_list), do: maybe_improper_list
+ @spec tl(maybe_improper_list) :: maybe_improper_list
def tl(list) do
:erlang.tl(list)
end
@@ -1151,23 +1151,23 @@ defmodule Kernel do
trunc(5.5) #=> 5
"""
- @spec trunc(number), do: integer
+ @spec trunc(number) :: integer
def trunc(number) do
:erlang.trunc(number)
end
@doc """
Returns the size of a tuple.
"""
- @spec tuple_size(tuple), do: non_neg_integer
+ @spec tuple_size(tuple) :: non_neg_integer
def tuple_size(tuple) do
:erlang.tuple_size(tuple)
end
@doc """
Converts a tuple to a list.
"""
- @spec tuple_to_list(tuple), do: list
+ @spec tuple_to_list(tuple) :: list
def tuple_to_list(tuple) do
:erlang.tuple_to_list(tuple)
end
@@ -1412,7 +1412,7 @@ defmodule Kernel do
For example, assuming the `Config` record defined above, it could be used
in typespecs as follow:
- @spec handle_config(Config.t), do: boolean()
+ @spec handle_config(Config.t) :: boolean()
Inside the record definition, a developer can define his own types too:
@@ -1681,7 +1681,7 @@ defmodule Kernel do
As in records, defining a protocol automatically defines a type named `t`,
which can be used as:
- @spec present?(Blank.t), do: boolean
+ @spec present?(Blank.t) :: boolean
def present?(blank) do
not Blank.blank?(blank)
end
@@ -2725,7 +2725,7 @@ defmodule Kernel do
end
"""
- @spec raise(binary | atom | tuple), do: no_return
+ @spec raise(binary | atom | tuple) :: no_return
def raise(msg) when is_binary(msg) do
:erlang.error RuntimeError.new(message: msg)
end
@@ -2751,7 +2751,7 @@ defmodule Kernel do
raise ArgumentError, message: "Sample"
"""
- @spec raise(tuple | atom, list), do: no_return
+ @spec raise(tuple | atom, list) :: no_return
def raise(exception, args) do
:erlang.error exception.exception(args)
end
@@ -2767,7 +2767,7 @@ defmodule Kernel do
it is not loaded. Check `Code.ensure_loaded/1` for more
information.
"""
- @spec function_exported?(atom | tuple, atom, integer), do: boolean
+ @spec function_exported?(atom | tuple, atom, integer) :: boolean
def function_exported?(module, function, arity) do
case is_tuple(module) do
true ->
View
69 lib/elixir/lib/kernel/typespec.ex
@@ -55,19 +55,35 @@ defmodule Kernel.Typespec do
end
end
+ @doc false
+ defmacro defspec(spec, [do: block]) do
+ IO.write "[WARNING] @spec f(...), do: type is deprecated, use @spec f(...) :: type\n"
+ quote do
+ Kernel.Typespec.defspec(:spec, (quote line: :keep, do: unquote(spec) :: unquote(block)), __ENV__)
+ end
+ end
+
+
@doc """
Defines a spec.
This macro is the one responsible to handle the attribute @spec.
## Examples
- @spec add(number, number), do: number
+ @spec add(number, number) :: number
"""
- defmacro defspec(spec, block) do
+ defmacro defspec(spec) do
quote do
- Kernel.Typespec.defspec(:spec, (quote line: :keep, do: unquote spec),
- (quote line: :keep, do: unquote block), __ENV__)
+ Kernel.Typespec.defspec(:spec, (quote line: :keep, do: unquote spec), __ENV__)
+ end
+ end
+
+ @doc false
+ defmacro defcallback(spec, [do: block]) do
+ IO.write "[WARNING] @callback f(...), do: type is deprecated, use @callback f(...) :: type\n"
+ quote do
+ Kernel.Typespec.defspec(:callback, (quote line: :keep, do: unquote(spec) :: unquote(block)), __ENV__)
end
end
@@ -77,13 +93,13 @@ defmodule Kernel.Typespec do
## Examples
- @callback add(number, number), do: number
+ @callback add(number, number) :: number
"""
- defmacro defcallback(spec, block) do
+ defmacro defcallback(spec) do
+ IO.write "[WARNING] @callback f(...), do: type is deprecated, use @callback f(...) :: type\n"
quote do
- Kernel.Typespec.defspec(:callback, (quote line: :keep, do: unquote spec),
- (quote line: :keep, do: unquote block), __ENV__)
+ Kernel.Typespec.defspec(:callback, (quote line: :keep, do: unquote(spec)), __ENV__)
end
end
@@ -169,7 +185,7 @@ defmodule Kernel.Typespec do
def spec_to_ast(name, { :type, line, :bounded_fun, [{ :type, _, :fun, [{ :type, _, :product, args }, result] }, constraints] }) do
[h|t] =
lc {:type, line, :constraint, [{:atom, _, :is_subtype}, [var, type]]} inlist constraints do
- { :::, line, [typespec_to_ast(var), typespec_to_ast(type)] }
+ { :is_subtype, line, [typespec_to_ast(var), typespec_to_ast(type)] }
end
args = lc arg inlist args, do: typespec_to_ast(arg)
@@ -304,7 +320,7 @@ defmodule Kernel.Typespec do
end
@doc false
- def defspec(type, {:when, _, [{ name, line, args }, constraints_guard] }, [do: return], caller) do
+ def defspec(type, {:when, _, [{ name, line, args }, {:::, _, [constraints_guard, return]}] }, caller) do
if is_atom(args), do: args = []
constraints = guard_to_constraints(constraints_guard, caller)
spec = { :type, line, :fun, fn_args(line, args, return, Keyword.keys(constraints), caller) }
@@ -314,7 +330,7 @@ defmodule Kernel.Typespec do
code
end
- def defspec(type, { name, line, args }, [do: return], caller) do
+ def defspec(type, {:::, _, [{ name, line, args }, return]}, caller) do
if is_atom(args), do: args = []
spec = { :type, line, :fun, fn_args(line, args, return, [], caller) }
code = { { name, Kernel.length(args) }, spec }
@@ -362,11 +378,11 @@ defmodule Kernel.Typespec do
defp typespec_to_ast({ :type, line, :fun, [{:type, _, :product, args},result] }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
- { :fun, line, args ++ [[do: typespec_to_ast(result)]] }
+ { :"::", line, [{ :fun, line, args }, typespec_to_ast(result)] }
end
defp typespec_to_ast({ :type, line, :fun, [] }) do
- { :fun, line, [] }
+ typespec_to_ast({ :type, line, :fun, [{:type, line, :product, [:'...']}, :any] })
end
defp typespec_to_ast({ :type, line, name, args }) do
@@ -436,6 +452,16 @@ defmodule Kernel.Typespec do
typespec(atom, vars, caller)
end
+ # Handle funs
+ defp typespec({:fun, line, args} = f, vars, caller) when is_list(args) do
+ IO.write "[WARNING] 'any' fun() type is deprecated, use fun(...) :: any instead\n"
+ typespec({:"::", line, [f, quote do: any]}, vars, caller)
+ end
+ defp typespec({:"::", line, [{:fun, _, arguments}, return]}, vars, caller) when is_list(arguments) do
+ args = fn_args(line, arguments, return, vars, caller)
+ { :type, line, :fun, args }
+ end
+
# Handle type operator
defp typespec({:"::", line, [var, expr] }, vars, caller) do
left = typespec(var, [elem(var, 0)|vars], caller)
@@ -476,18 +502,6 @@ defmodule Kernel.Typespec do
{ :type, line, :tuple, args }
end
- # Handle funs
- defp typespec({:fun, line, arguments}, vars, caller) when is_list(arguments) do
- args =
- case :lists.reverse(arguments) do
- [[{:do,h}]|t] -> fn_args(line, :lists.reverse(t), h, vars, caller)
- [] -> []
- _ -> [fn_args(line, arguments, vars, caller)]
- end
-
- { :type, line, :fun, args }
- end
-
# Handle variables or local calls
defp typespec({name, line, atom}, vars, caller) when is_atom(atom) do
if List.member?(vars, name) do
@@ -550,7 +564,10 @@ defmodule Kernel.Typespec do
defp collect_union(v), do: [v]
defp fn_args(line, args, return, vars, caller) do
- [fn_args(line, args, vars, caller), typespec(return, vars, caller)]
+ case [fn_args(line, args, vars, caller), typespec(return, vars, caller)] do
+ [{:type,_,:any},{:type,_,:any,[]}] -> []
+ x -> x
+ end
end
defp fn_args(line, [{:"...", _, _}], _vars, _caller) do
View
38 lib/elixir/lib/keyword.ex
@@ -22,7 +22,7 @@ defmodule Keyword do
which behaves as a dict, `Keyword.from_enum` do not remove
duplicated entries.
"""
- @spec from_enum(Enum.t), do: t
+ @spec from_enum(Enum.t) :: t
def from_enum(enum) when is_list(enum) do
enum
end
@@ -34,7 +34,7 @@ defmodule Keyword do
@doc """
Checks if the given argument is a keywords list or not
"""
- @spec keyword?(term), do: boolean
+ @spec keyword?(term) :: boolean
def keyword?([{ key, _value } | rest]) when is_atom(key) do
case atom_to_list(key) do
'Elixir-' ++ _ -> false
@@ -48,7 +48,7 @@ defmodule Keyword do
@doc """
Returns an empty keyword list, i.e. an empty list.
"""
- @spec new, do: t
+ @spec new :: t
def new do
[]
end
@@ -63,7 +63,7 @@ defmodule Keyword do
#=> [a: 2, b: 1]
"""
- @spec new(Enum.t), do: t
+ @spec new(Enum.t) :: t
def new(pairs) do
Enum.reduce pairs, [], fn { k, v }, keywords ->
put(keywords, k, v)
@@ -81,7 +81,7 @@ defmodule Keyword do
#=> [a: :a, b: :b]
"""
- @spec new(Enum.t, fun({key, value}, do: {key, value})), do: t
+ @spec new(Enum.t, fun({key, value}) :: {key, value}) :: t
def new(pairs, transform) do
Enum.reduce pairs, [], fn i, keywords ->
{ k, v } = transform.(i)
@@ -105,8 +105,8 @@ defmodule Keyword do
Keyword.get [a: 1], :b, 3 #=> 3
"""
- @spec get(t, key), do: value
- @spec get(t, key, value), do: value
+ @spec get(t, key) :: value
+ @spec get(t, key, value) :: value
def get(keywords, key, default // nil) when is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{ ^key, value } -> value
@@ -124,7 +124,7 @@ defmodule Keyword do
Keyword.get! [a: 1], :b #=> raises KeyError[key: :b]
"""
- @spec get!(t, key), do: value | no_return
+ @spec get!(t, key) :: value | no_return
def get!(keywords, key) when is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{ ^key, value } -> value
@@ -141,7 +141,7 @@ defmodule Keyword do
#=> [1,2]
"""
- @spec get_values(t, key), do: [value]
+ @spec get_values(t, key) :: [value]
def get_values(keywords, key) when is_atom(key) do
lc { k, v } inlist keywords, key == k, do: v
end
@@ -155,7 +155,7 @@ defmodule Keyword do
Keyword.keys [a: 1, b: 2] #=> [:a,:b]
"""
- @spec keys(t), do: [key]
+ @spec keys(t) :: [key]
def keys(keywords) do
lc { key, _ } inlist keywords, do: key
end
@@ -168,7 +168,7 @@ defmodule Keyword do
Keyword.values [a: 1, b: 2] #=> [1,2]
"""
- @spec values(t), do: [value]
+ @spec values(t) :: [value]
def values(keywords) do
lc { _, value } inlist keywords, do: value
end
@@ -185,7 +185,7 @@ defmodule Keyword do
Keyword.delete [b: 2], :a #=> [b: 2]
"""
- @spec delete(t, key), do: t
+ @spec delete(t, key) :: t
def delete(keywords, key) when is_atom(key) do
lc { k, _ } = tuple inlist keywords, key != k, do: tuple
end
@@ -202,7 +202,7 @@ defmodule Keyword do
#=> [a: 3, b: 2]
"""
- @spec put(t, key, value), do: t
+ @spec put(t, key, value) :: t
def put(keywords, key, value) when is_atom(key) do
[{key, value}|delete(keywords, key)]
end
@@ -217,7 +217,7 @@ defmodule Keyword do
#=> true
"""
- @spec equal?(t, t), do: boolean
+ @spec equal?(t, t) :: boolean
def equal?(left, right) do
:lists.sort(left) == :lists.sort(right)
end
@@ -232,7 +232,7 @@ defmodule Keyword do
#=> [a:3, b:2, d: 4]
"""
- @spec merge(t, t), do: t
+ @spec merge(t, t) :: t
def merge(d1, d2) do
d2 ++ lc({ k, _ } = tuple inlist d1, not has_key?(d2, k), do: tuple)
end
@@ -249,7 +249,7 @@ defmodule Keyword do
#=> [a:4, b:2, d: 4]
"""
- @spec merge(t, t, fun(key, value, value, do: value)), do: t
+ @spec merge(t, t, fun(key, value, value) :: value) :: t
def merge(d1, d2, fun) do
do_merge(d2, d1, fun)
end
@@ -273,7 +273,7 @@ defmodule Keyword do
#=> false
"""
- @spec has_key?(t, key), do: boolean
+ @spec has_key?(t, key) :: boolean
def has_key?(keywords, key) when is_atom(key) do
:lists.keymember(key, 1, keywords)
end
@@ -290,7 +290,7 @@ defmodule Keyword do
#=> KeyError
"""
- @spec update(t, key, fun(value, do: value)), do: t | no_return
+ @spec update(t, key, fun(value) :: value) :: t | no_return
def update([{key, value}|keywords], key, fun) do
[{key, fun.(value)}|delete(keywords, key)]
end
@@ -315,7 +315,7 @@ defmodule Keyword do
#=> [a: 1, b: 11]
"""
- @spec update(t, key, value, fun(value, do: value)), do: t
+ @spec update(t, key, value, fun(value) :: value) :: t
def update([{key, value}|keywords], key, _initial, fun) do
[{key, fun.(value)}|delete(keywords, key)]
end
View
2 lib/elixir/lib/protocol.ex
@@ -393,7 +393,7 @@ defmodule Protocol.DSL do
# Convert the spec to callback if possible,
# otherwise generate a dummy callback
Protocol.DSL.callback_from_spec(__MODULE__, name, arity) ||
- @callback unquote(name)(unquote_splicing(type_args)), do: term
+ @callback unquote(name)(unquote_splicing(type_args)) :: term
end
end
end
View
24 lib/elixir/lib/record.ex
@@ -483,18 +483,18 @@ defmodule Record do
@type t :: { __MODULE__, unquote_splicing(types) }
end
- @spec new, do: t
- @spec new(Keyword.t | tuple), do: t
- @spec to_keywords(t), do: Keyword.t
- @spec update(Keyword.t, t), do: t
- @spec __index__(atom), do: non_neg_integer | nil
+ @spec new :: t
+ @spec new(Keyword.t | tuple) :: t
+ @spec to_keywords(t) :: Keyword.t
+ @spec update(Keyword.t, t) :: t
+ @spec __index__(atom) :: non_neg_integer | nil
end
end
defp accessor_specs([{ key, _default, spec }|t], acc) do
contents = quote do
- @spec unquote(key)(t), do: unquote(spec)
- @spec unquote(key)(unquote(spec), t), do: t
+ @spec unquote(key)(t) :: unquote(spec)
+ @spec unquote(key)(unquote(spec), t) :: t
end
accessor_specs(t, [contents | acc])
end
@@ -637,7 +637,7 @@ defmodule Record.Extensions do
update = prefix("update_", key)
quote do
- @spec unquote(update)(fun(unquote(spec), do: unquote(spec)), t), do: t
+ @spec unquote(update)(fun(unquote(spec) :: unquote(spec)), t) :: t
end
end
@@ -646,24 +646,24 @@ defmodule Record.Extensions do
merge = prefix("merge_", key)
quote do
- @spec unquote(prepend)(list, t), do: t
- @spec unquote(merge)(Keyword.t, t), do: t
+ @spec unquote(prepend)(list, t) :: t
+ @spec unquote(merge)(Keyword.t, t) :: t
end
end
defp extension_specs_for(key, default, _spec) when is_number(default) do
increment = prefix("increment_", key)
quote do
- @spec unquote(increment)(number, t), do: t
+ @spec unquote(increment)(number, t) :: t
end
end
defp extension_specs_for(key, default, _spec) when is_boolean(default) do
toggle = prefix("toggle_", key)
quote do
- @spec unquote(toggle)(t), do: t
+ @spec unquote(toggle)(t) :: t
end
end
View
46 lib/elixir/lib/string.ex
@@ -30,7 +30,7 @@ defmodule String do
String.printable?("abc") #=> true
"""
- @spec printable?(t), do: boolean
+ @spec printable?(t) :: boolean
# Allow basic ascii chars
def printable?(<<c, t :: binary>>) when c in ?\s..?~ do
printable?(t)
@@ -128,9 +128,9 @@ defmodule String do
String.split("a,b", %r{\.}) #=> ["a,b"]
"""
- @spec split(t), do: [t]
- @spec split(t, t | [t] | Regex.t), do: [t]
- @spec split(t, t | [t] | Regex.t, Keyword.t), do: [t]
+ @spec split(t) :: [t]
+ @spec split(t, t | [t] | Regex.t) :: [t]
+ @spec split(t, t | [t] | Regex.t, Keyword.t) :: [t]
def split(binary, pattern // " ", options // [])
def split(binary, pattern, options) when is_regex(pattern) do
@@ -156,7 +156,7 @@ defmodule String do
String.upcase("josé") #=> "JOSÉ"
"""
- @spec upcase(t), do: t
+ @spec upcase(t) :: t
defdelegate upcase(binary), to: String.Unicode
@doc """
@@ -173,7 +173,7 @@ defmodule String do
String.downcase("JOSÉ") #=> "josé"
"""
- @spec downcase(t), do: t
+ @spec downcase(t) :: t
defdelegate downcase(binary), to: String.Unicode
@doc """
@@ -185,7 +185,7 @@ defmodule String do
String.rstrip(" abc ") #=> " abc"
"""
- @spec rstrip(t), do: t
+ @spec rstrip(t) :: t
def rstrip(""), do: ""
@@ -217,7 +217,7 @@ defmodule String do
String.rstrip(" abc _", ?_) #=> " abc "
"""
- @spec rstrip(t, char), do: t
+ @spec rstrip(t, char) :: t
def rstrip("", _char), do: ""
@@ -253,7 +253,7 @@ defmodule String do
String.lstrip(" abc ") #=> "abc "
"""
- @spec lstrip(t), do: t
+ @spec lstrip(t) :: t
def lstrip(<<char, rest :: binary>>) when char in @whitespace do
lstrip(rest)
@@ -272,7 +272,7 @@ defmodule String do
"""
- @spec lstrip(t, char), do: t
+ @spec lstrip(t, char) :: t
def lstrip(<<char, rest :: binary>>, char) do
<<lstrip(rest, char) :: binary>>
@@ -291,7 +291,7 @@ defmodule String do
String.strip(" abc ") #=> "abc"
"""
- @spec strip(t), do: t
+ @spec strip(t) :: t
def strip(string) do
rstrip(lstrip(string))
@@ -306,7 +306,7 @@ defmodule String do
String.strip("a abc a", ?a) #=> " abc "
"""
- @spec strip(t, char), do: t
+ @spec strip(t, char) :: t
def strip(string, char) do
rstrip(lstrip(string, char), char)
@@ -331,8 +331,8 @@ defmodule String do
String.replace("a,b,c", ",", "[]", insert_replaced: [1,1]) #=> "a[,,]b[,,]c"
"""
- @spec replace(t, t, t), do: t
- @spec replace(t, t, t, Keyword.t), do: t
+ @spec replace(t, t, t) :: t
+ @spec replace(t, t, t, Keyword.t) :: t
def replace(subject, pattern, replacement, options // []) do
opts = translate_replace_options(options)
@@ -358,7 +358,7 @@ defmodule String do
String.duplicate("abc", 2) #=> "abcabc"
"""
- @spec duplicate(t, pos_integer), do: t
+ @spec duplicate(t, pos_integer) :: t
def duplicate(subject, n) when is_integer(n) and n > 0 do
:binary.copy(subject, n)
end
@@ -373,7 +373,7 @@ defmodule String do
String.codepoints("ἅἪῼ") #=> ["ἅ","Ἢ","ῼ"]
"""
- @spec codepoints(t), do: [codepoint]
+ @spec codepoints(t) :: [codepoint]
defdelegate codepoints(string), to: String.Unicode
@doc """
@@ -388,7 +388,7 @@ defmodule String do
String.next_codepoint("josé") #=> { "j", "osé" }
"""
- @spec next_codepoint(t), do: codepoint | :no_codepoint
+ @spec next_codepoint(t) :: codepoint | :no_codepoint
defdelegate next_codepoint(string), to: String.Unicode
@doc """
@@ -398,7 +398,7 @@ defmodule String do
String.graphemes("Ā̀stute") # => ["Ā̀","s","t","u","t","e"]
"""
- @spec graphemes(t), do: [grapheme]
+ @spec graphemes(t) :: [grapheme]
defdelegate graphemes(string), to: String.Unicode
@doc """
@@ -413,7 +413,7 @@ defmodule String do
String.next_grapheme("josé") #=> { "j", "osé" }
"""
- @spec next_grapheme(t), do: grapheme | :no_grapheme
+ @spec next_grapheme(t) :: grapheme | :no_grapheme
defdelegate next_grapheme(string), to: String.Unicode
@doc """
@@ -425,7 +425,7 @@ defmodule String do
String.first("եոգլի") #=> "ե"
"""
- @spec first(t), do: grapheme
+ @spec first(t) :: grapheme
def first(string) do
case next_grapheme(string) do
{ char, _ } -> char
@@ -442,7 +442,7 @@ defmodule String do
String.last("եոգլի") #=> "ի"
"""
- @spec last(t), do: grapheme
+ @spec last(t) :: grapheme
def last(string) do
do_last(next_grapheme(string), "")
end
@@ -462,7 +462,7 @@ defmodule String do
String.length("եոգլի") #=> 5
"""
- @spec length(t), do: non_neg_integer
+ @spec length(t) :: non_neg_integer
def length(string) do
do_length(next_grapheme(string))
end
@@ -486,7 +486,7 @@ defmodule String do
String.at("elixir", -10) #=> nil
"""
- @spec at(t, integer), do: grapheme | nil
+ @spec at(t, integer) :: grapheme | nil
def at(string, position) when position >= 0 do
do_at(next_grapheme(string), position, 0)
end
View
2 lib/elixir/lib/system.ex
@@ -169,7 +169,7 @@ defmodule System do
System.halt(:abort)
"""
- @spec halt(non_neg_integer | List.Chars.t | :abort, [] | [flush: false]), do: no_return
+ @spec halt(non_neg_integer | List.Chars.t | :abort, [] | [flush: false]) :: no_return
def halt(status // 0, options // [])
def halt(status, options) when is_integer(status) or status == :abort do
View
2 lib/elixir/test/elixir/protocol_test.exs
@@ -8,7 +8,7 @@ end
defprotocol ProtocolTest.WithExcept do
@except [Atom, Number, List]
- @spec blank(t), do: boolean
+ @spec blank(t) :: boolean
def blank(thing)
end
View
47 lib/elixir/test/elixir/typespec_test.exs
@@ -123,14 +123,14 @@ defmodule Typespec.Test.Type do
test "@type with a fun" do
spec = test_module do
- @type mytype :: fun
+ @type mytype :: fun(...) :: any
end
assert {:mytype,{:type,_,:fun, []},[]} = spec
end
test "@type with a fun with arguments and return type" do
spec = test_module do
- @type mytype :: fun(integer, integer, do: integer)
+ @type mytype :: fun(integer, integer) :: integer
end
assert {:mytype,{:type,_,:fun, [{:type, _, :product,
[{:type, _, :integer, []}, {:type, _, :integer, []}]},
@@ -139,15 +139,15 @@ defmodule Typespec.Test.Type do
test "@type with a fun with no arguments and return type" do
spec = test_module do
- @type mytype :: fun(do: integer)
+ @type mytype :: fun() :: integer
end
assert {:mytype,{:type,_,:fun, [{:type, _, :product, []},
{:type, _, :integer, []}]}, []} = spec
end
test "@type with a fun with any arity and return type" do
spec = test_module do
- @type mytype :: fun(..., do: integer)
+ @type mytype :: fun(...) :: integer
end
assert {:mytype,{:type,_,:fun, [{:type, _, :any},
{:type, _, :integer, []}]}, []} = spec
@@ -186,7 +186,7 @@ defmodule Typespec.Test.Type do
test "@type with annotations" do
{spec1, spec2} = test_module do
t1 = @type mytype :: named :: integer
- t2 = @type mytype1 :: fun(a :: integer, do: integer)
+ t2 = @type mytype1 :: fun(a :: integer) :: integer
{t1,t2}
end
assert {:mytype, {:ann_type, _, [{:var, _, :named}, {:type, _, :integer, []}]}, []} = spec1
@@ -224,9 +224,9 @@ defmodule Typespec.Test.Type do
def myfun(x), do: x
def myfun(), do: :ok
def myfun(x,y), do: {x,y}
- t1 = @spec myfun(integer), do: integer
- t2 = @spec myfun(), do: integer
- t3 = @spec myfun(integer, integer), do: {integer, integer}
+ t1 = @spec myfun(integer) :: integer
+ t2 = @spec myfun() :: integer
+ t3 = @spec myfun(integer, integer) :: {integer, integer}
{t1,t2,t3}
end
assert {{:myfun,1},{:type,_,:fun,[{:type,_,:product,[{:type,_,:integer,[]}]},{:type,_,:integer,[]}]}} = spec1
@@ -235,21 +235,18 @@ defmodule Typespec.Test.Type do
end
test "@spec(spec) with guards" do
- {spec1, spec2} = test_module do
+ spec1 = test_module do
def myfun(x), do: x
- t1 = @spec myfun(x) when is_subtype(x, integer), do: boolean
- t2 = @spec myfun(x) when x :: integer, do: boolean
- {t1,t2}
+ @spec myfun(x) when is_subtype(x, integer) :: boolean
end
assert {{:myfun,1},{:type,_,:bounded_fun,[{:type,_,:fun,[{:type,_,:product,[{:var,_,:x}]},{:type,_,:boolean,[]}]},[{:type,_,:constraint,[{:atom,_,:is_subtype},[{:var,_,:x},{:type,_,:integer,[]}]]}]]}} = spec1
- assert {{:myfun,1},{:type,_,:bounded_fun,[{:type,_,:fun,[{:type,_,:product,[{:var,_,:x}]},{:type,_,:boolean,[]}]},[{:type,_,:constraint,[{:atom,_,:is_subtype},[{:var,_,:x},{:type,_,:integer,[]}]]}]]}} = spec2
end
test "@callback(callback)" do
{spec1, spec2, spec3} = test_module do
- t1 = @callback myfun(integer), do: integer
- t2 = @callback myfun(), do: integer
- t3 = @callback myfun(integer, integer), do: {integer, integer}
+ t1 = @callback myfun(integer) :: integer
+ t2 = @callback myfun() :: integer
+ t3 = @callback myfun(integer, integer) :: {integer, integer}
{t1,t2,t3}
end
assert {{:myfun,1},{:type,_,:fun,[{:type,_,:product,[{:type,_,:integer,[]}]},{:type,_,:integer,[]}]}} = spec1
@@ -260,9 +257,9 @@ defmodule Typespec.Test.Type do
test "@spec + @callback" do
{ specs, callbacks } = test_module do
def myfun(x), do: x
- @spec myfun(integer), do: integer
+ @spec myfun(integer) :: integer
@spec myfun(char_list), do: char_list
- @callback cb(integer), do: integer
+ @callback cb(integer) :: integer
{ @spec, @callback }
end
@@ -293,7 +290,7 @@ defmodule Typespec.Test.Type do
(quote do: @type binary_type2() :: <<_ :: 3 * 8>>),
(quote do: @type binary_type3() :: <<_ :: 3>>),
(quote do: @type tuple_type() :: {integer()}),
- (quote do: @type ftype() :: fun() | fun(do: integer()) | fun(integer(), do: integer())),
+ (quote do: @type ftype() :: fun() :: any() | fun() :: integer() | fun(integer()) :: integer()),
]
types = test_module do
@@ -325,9 +322,9 @@ defmodule Typespec.Test.Type do
test "spec_to_ast" do
specs = [
- (quote do: @spec a(), do: integer()),
- (quote do: @spec a(atom()), do: integer()),
- (quote do: @spec a(b) when b :: integer</