Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the typespec declaration for a function that may raise?

Tags:

elixir

Using Elixir's typespecs, how do I document in @spec that a function may raise an exception instead of returning a value?

like image 394
Nathan Long Avatar asked Mar 05 '18 13:03

Nathan Long


People also ask

What is a Typespec?

Types and specs. Elixir is a dynamically typed language, so all types in Elixir are checked at runtime. Nonetheless, Elixir comes with typespecs, which are a notation used for: declaring typed function signatures (also called specifications); declaring custom types.

What is @spec in Elixir?

For this purpose Elixir has @spec annotation to describe the specification of a function that will be checked by compiler. However in some cases specification is going to be quite big and complicated. If you would like to reduce complexity, you want to introduce a custom type definition.


1 Answers

Nothing

At one time, no_return() (listed in the Typespecs docs) was used in various places to document that a function might raise an exception - eg, File.copy!/2 in Elixir 1.6.

But those uses were removed by Devon Estes in this PR, and in approving it, José Valim said:

Any function can fail any time in face of invalid input.

I take this to mean that "this function could possibly raise an error" is always assumed and need not be documented in the typespec.

like image 193
Nathan Long Avatar answered Oct 17 '22 03:10

Nathan Long