Erlang phylosophy: Should I allow users to deal with incorrect input or not?

In this case:

% print/1: Prints out the integers between 1 and N print(0) -> io:format("~w~n", [0]); print(N) when is_integer(N) -> io:format("~w~n", [N]), print(N - 1). 

If the user enters a non-integer, this happens:

 11> effects:print('alfalfa'). ** exception error: no function clause matching effects:print(alfalfa) 

About philosophy: Should I correct my program in such a way as to “catch all” types of input?

 % print/1: Prints out the integers between 1 and N print(0) -> io:format("~w~n", [0]); print(N) when is_integer(N) -> io:format("~w~n", [N]), print(N - 1). % Last Line added: print(_Other) -> false. 

I am new to erlang. Is there any agreement on this?

Thanks!

+4
source share
2 answers

In Erlang, you basically won't catch such bad APIs. If the template does not match the call, a class exception will be thrown with a fairly detailed message ( {function_clause, CallStack} ). Almost every standard library method throws. At the moment, I do not think about counterexamples.

Btw: you usually returned {error, Msg} , not false if there was some kind of error (this is mainly a usage error). In good cases, ok or {ok, Datum} will be returned.

+7
source

Source: https://habr.com/ru/post/1380081/


All Articles