Skip to content

Instantly share code, notes, and snippets.

@zoldar
Last active December 27, 2019 19:07
Show Gist options
  • Select an option

  • Save zoldar/da7d75274df01e81ad392e128dcae52d to your computer and use it in GitHub Desktop.

Select an option

Save zoldar/da7d75274df01e81ad392e128dcae52d to your computer and use it in GitHub Desktop.
add(M1, M2) ->
case {M1, M2} of
{{money, Cents1, Currency}, {money, Cents2, Currency1}} ->
{ok, {money, Cents1 + Cents2, Currency1}};
{{money, _, C1}, {money, _, C2}} ->
{error, {currencies_mismatch, C1, C2}}
end.
pub type Currency {
USD
EUR
PLN
}
pub type Money {
Money(cents: Int, currency: Currency)
}
pub type MoneyError {
CurrenciesMismatch(c1: Currency, c2: Currency)
}
pub fn add(m1: Money, m2: Money) {
case m1, m2 {
Money(cents1, currency), Money(cents2, currency) -> Ok(Money(cents1 + cents2, currency))
Money(_, c1), Money(_, c2) -> Error(CurrenciesMismatch(c1, c2))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment