📜 ⬆️ ⬇️

Erlang for the little ones. Chapter 4: Type System

image Good Monday, Habr! We continue to study Erlang for the little ones.

In the last chapter we dealt with the syntax of functions. In this chapter, we will introduce the language type system.



Dynamic strong typing


If you read past chapters, then, most likely, you noticed that we did not specify the types of variables and the values ​​returned by the functions. We also did not specify the data type when comparing with the sample. For example, the tuple {A, B} can be {someAtom, 100} , {100.0, "Some string"} , etc.
')
If the data cannot be matched to the specified sample, an error will be generated. This will happen immediately at the time the code is executed. This is because Erlang is a dynamically typed language. The compiler cannot catch all errors at the compilation stage. Therefore, many errors are generated during program execution.

Why does Erlang not have static typing? A lot of copies were broken in the debate about which typing is better: static or dynamic. Each has its pros and cons. One of the main drawbacks of dynamic typing is the impossibility of searching for errors associated with types at the compilation stage. Because of this, such errors occur in runtime and the program crashes, which reduces the overall reliability of the software product. But Erlang solves this problem in a different, more efficient way.

Erlang ideology: “If it broke, let it break.” The language has the tools to isolate and correctly handle all errors that may occur. This makes Erlang programs very robust. A classic example of the survivability of programs on Erlang, which is given - software switches Ericsson AXD 301 ATM. This product has more than a million lines of code and 99.9999999% uptime. Impressive.

In general, Erlang does not seek to avoid errors. He suggests to process them correctly.

Erlang also belongs to languages ​​with strong typing . This means that type conversions need to be made explicitly. If we use incompatible data types in one expression, we get an error:
 1> 10 - "5". ** exception error: bad argument in an arithmetic expression in operator -/2 called as 10 - "5" 


Type conversion


For type conversion in Erlang, standard library functions are used (found in the Erlang module). The names of these functions are formed as follows: <_>_to_<_> . Here is a complete list of such features:

And an example of their use:
 1> erlang:integer_to_list(123). "123" 2> erlang:atom_to_list(false). "false" 3> erlang:iolist_to_atom(123). ** exception error: bad argument in function iolist_to_atom/1 called as iolist_to_atom(123) ``` 


Type Check


Most data types in Erlang can be distinguished visually. Lists are enclosed in square brackets, tuples in curly, and strings in double quotes, etc. This provides the simplest type checking. For example, the pattern for matching [x|xs] can only be mapped to a list. Otherwise, the operation will fail.

But such elementary checks are not enough. Often you need to check what type this or that variable belongs to. For this, Erlang has a number of functions that take one argument (sometimes more) and if its type is as expected, return true , otherwise false . These functions can be used in security expressions and the type they test is clear from their name.

An interesting point: in Erlang there is no function that would return the type of the passed variable (like type_of() ). And at first glance it may seem strange, because one universal function is more convenient than a bunch of narrowly targeted ones. The answer to this question lies in the ideology of the language: the program should only do what is described explicitly. Everything else should lead to an error . This approach will be discussed in more detail in the chapter on error handling.

These data types can be combined with each other using lists and tuples to create more flexible and universal types.

To be completely honest, you need to mention that in Erlang there is an opportunity to explicitly specify types. An entire chapter will be allocated to this functionality, so there will not be a talk about it here.

Conclusion


This time it turned out brief and to the point. The theme is simple, but you need to highlight it.
Any comments and additions are waiting in the comments. About errors and typos, please report in a personal.

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


All Articles