Erlang Programming/Additional Types

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Additional Types[edit]

We have already seen the following types: tuple, list, integer, float, function and pid. We can check the type of an object by testing it.

1> is_pid( self() ).

If you wish to convert between types, lists are the lingua franca of types in Erlang so make it a list first on your way to something else. Remember, type conversion is not a parallel-safe operation.

Some additional types are: port and reference.

A port is a connection to the external world. Typically ports generate and/or consume bit streams. Binary data is considered untyped data in Erlang. (See BitSyntax).

A reference is a globally unique symbol and is generated with:

19> make_ref(). 

A reference is only useful when a unique tag is needed across all connected processes. Do not confuse the term reference with a references in C, which points to data. An Erlang reference is only a unique tag.

Erlang Has No Boolean Type[edit]

Erlang has no Boolean type. It has the atoms (true and false) which are generated by certain functions and expected by certain functions like guards and list comprehensions. The function: is_constant() generates either true or false.

We can test whether an object is a constant.

1> is_constant(a) 
2> is_atom(a).

Because atoms are represented as constant numbers, atoms are constants.

** 1: variable 'A' is unbound **
5> A=1.
6> is_constant(A).

Theoretically, because the Boolean type is not built in, it should make it easier to let Erlang compute with alternate types of logic, {true, false, null} for instance.