Erlang way of calling functions

Kostis Sagonas kostis@REDACTED
Tue Dec 7 16:44:00 CET 2021


On 12/7/21 1:41 PM, Γιάννης Γεωργίου wrote:
> 
> 
> I 'm new to erlang and I notice this way of calling functions..
> 
> {ok, Returndata} = module:function(Inputdata).
> 
> Which is the purpose of ok atom? How I can use it?


Strictly speaking, the function call is the part to the right of the = 
in the line you have written above.

The part to the left of the = is pattern matching: i.e., deconstructing 
and checking the return value of the function call.  In this particular 
case, and in many others that you've probably noticed in typical Erlang 
code, the pattern insists that the return of the function denotes that 
evaluation went OK, i.e., it did not result in any error.

There is nothing magical about the 'ok' atom, other than it's a standard 
convention used to differentiate the function return from returns that 
denote 'error's.  In other words it's quite typical, especially in 
library functions to return {'ok', SomeTerm(s)} for successful calls and 
{'error', Reason(s)} for the cases that some error condition was triggered.

Pattern matchings like the ones in the line above help you insist that 
whatever appears _after_ that line does not get executed in case things 
were not OK.

Hope this helps.

Kostis


More information about the erlang-questions mailing list