Last active
December 27, 2019 19:07
-
-
Save zoldar/da7d75274df01e81ad392e128dcae52d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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