Coder
Coder

Reputation: 77

Erlang Type Definition

I am new to Erlang and trying to figure out how to define types so that they can be used as building blocks for other complex types. For defniing a simple type

-type macaddress():: <<_:48>>.

can be used. But suppose we want to define the src and dst mac address and use the simple mac address as the basic building block and build upon it. Then how will we do that, would

-type srcmacaddress(Macaddress):: [{Macaddress}]. -type dstmacaddress(Macaddress)::[{Macaddress}].

be fine as I would like the Macaddress to be of the type macaddress defined before.

Let me know if you have any ideas and thanks.

Upvotes: 2

Views: 132

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170919

You use this type the same way you use the built-in types, writing macadress() as part of definition of the complex type:

-type srcmacaddress() :: [{macadress()}].

(Assuming you really want a list of single-element tuples where this single element is a macadress().)

Dumb use example:

-spec foo(macadress()) -> srcmacaddress().
foo(X) -> [{X}].

Upvotes: 4

Related Questions