Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "-spec" do in Erlang syntax? What is the diffrence if a function is created with or without -spec

Tags:

erlang

I am new to erlang programming. I have many doubts. One of them is use of -spec.

What does "-spec" do in Erlang syntax? What is the difference if a function is created with or without -spec

function without -spec

add(A, B) ->
    A + B.

function with -spec

-spec add(Number, Number).
add(A, B) ->
    A + B.

I searched on the google but unable to understand the exact use of the -spec. Can anyone please explain?

like image 360
elegant-user Avatar asked Feb 05 '23 05:02

elegant-user


1 Answers

spec adds up information about the code. It indicates the arity of the function and combined with -type declarations, are helpful for documentation and bug detection tools.

Tools like Edoc use these type specifications for building documentation. Tools like Dialyzer use it for static analysis of the code.

So it is not used directly by the running code but many tools use it for better "understanding" the code.

like image 199
Asier Azkuenaga Avatar answered Feb 09 '23 01:02

Asier Azkuenaga