Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there () or parenthesis at the end of the type specs of elixir functions?

Tags:

elixir

For example, the type spec of flat_map is below.

flat_map(Enumerable.t(), (element() -> Enumerable.t())) :: Enumerable.t()

There are many parenthesis at the end of input and output data type. Why are there?

In other languages such as TypeScript, Haskell, or Rust, data types are just types without ().

Thanks in advance.

like image 455
주피터 Avatar asked Oct 30 '25 14:10

주피터


2 Answers

Because some of the types can be parameterised, so you can specify a list of integers as such: list(integer).

like image 93
Martin Gausby Avatar answered Nov 04 '25 07:11

Martin Gausby


In case someone need it. We can choose both ways to type () or not on functions with zero arity and types, in specs

Remember, this is works only for specs

  • it is shorter and a bit readable
  • ambiguity is possible in some rare cases

Mix format doesnt add () for specs

No additional warning emitted with dialyzer

No additional warning emitted during app compiling

for more information, check out hexdocs - they are using both options in @spec https://hexdocs.pm/elixir/typespecs.html

like image 29
Drilla Avatar answered Nov 04 '25 07:11

Drilla