Search code examples
typeserlangdefault-valuerecordsrestrictions

Erlang records with both type and value restrictions as well as default values


I am trying to write a record which represents a bank account:

-record(account, {  name :: atom(),
                    type :: atom(),
                    balance = 0 :: integer()  }).

I also want to restrict the balance to always be >= 0. How do I do this?


Solution

  • Something like balance = 0 :: 0 | pos_integer() might do the trick.

    edit wasn't sure it existed, but non_neg_integer() would be better :

    balance = 0 :: non_neg_integer()