Posts Tagged ‘typing’
In this post I will present you the data typing in Erlang.
Erlang is a dynamic typing programming language. That means that when “declaring” a variable you do not need to statically specify the type it will be (as in static typing languages). Read more about dynamic typing here. For example, this is how you would declare and initialize an integer in C (static typing):
int i = 17;
while in Erlang you would do: (we will see how to declare variables in a while)
I = 17.
This approach has both advantages and disadvantages. For example, when programming, it is fast and convenient not to declare the variables’ types, but in big projects it can lead to code readability problems unless well documented.
Erlang is heavily influnced by Prolog, so Prolog programmers should be ready to discover several commonalities. As with Prolog, the variable declaration is implicit; every character sequence generated by the regular expression
[A-Z_]([0-9a-zA-Z_]*) is a variable. Some examples are
A, Ab, A_b, Ab1, _, _A_b3. Just to mention that the ones starting with “_” has a special meaning; they are “don’t care” variables. When you use them, the value they will take will probably not be used later. Something like place keepers. You will meet them again and again, so don’t worry for now.
Another importand characteristic that Erlang inherited from Prolog is binding with pattern matching. In a nutshell, a value is not assigned to a variable, but bound with pattern matching. The most important consequence of this is that variables in Erlang are single assignement. Once bound to a value, their value cannot change for their lifetime. For example, in an emulator try the following:
1> A = 10. 10 2> A = 11.
The output you will get is:
** exception error: no match of right hand side value 11
What happens is that
A is bound to the value 10, so Erlang tries to pattern match 10 with the value 11, which is impossible.
Since I want to keep the posts rather small, I will break this one here and I will present the actual Erlang types on the following post(s).