주피터
주피터

Reputation: 135

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

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.

Upvotes: 4

Views: 505

Answers (2)

Drilla
Drilla

Reputation: 119

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

Upvotes: 2

Martin Gausby
Martin Gausby

Reputation: 174

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

Upvotes: 9

Related Questions