[erlang-questions] Noob question about records

Steve Vinoski vinoski@REDACTED
Sun Apr 26 04:54:04 CEST 2009


On 4/25/09, Larry White <ljw1001@REDACTED> wrote:
>  -record(money, { currency = usd, amount = 0 }).
>
>  add(Money1, Money2) ->
>         #money{currency=Cur1, amount=Amt1} = Money1.
>         #money{currency=Cur2, amount=Amt2} = Money2.
>         %% TODO: Check to see that the currencies are the same and throw
>  exception if not
>         Total = Amt1 + Amt2.
>         #money{currency=Cur1,amount=
> Total}.

Note that you don't need to check the currencies and throw an
exception in the manner your comment suggests. Do this instead:

add(#money{currency = C, amount = Amt1}, #money{currency = C, amount = Amt2}) ->
    #money{currency = C, amount = Amt1 + Amt2}.

Let pattern matching do the work for you.

--steve



More information about the erlang-questions mailing list