Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing Values #1

Closed
xaepstudio opened this issue Jul 22, 2018 · 4 comments
Closed

Initializing Values #1

xaepstudio opened this issue Jul 22, 2018 · 4 comments

Comments

@xaepstudio
Copy link

Hello, great resource!

We're building a localizable app that uses Money and Currency Extensions but not sure of the correct way to do so.

Question: All examples show variable initialization with the currency specified. Examples:

  • "let euros: Money = 123.45"
  • "let prices: [Money] = [2.19, 5.39, 20.99, 2.99, 1.99, 1.99, 0.99]"

Is it possible to initialize variables to the default/local currency -- without specifying the currency in the variable init statement? For example:

  • "let locale = Locale.current"
  • "let currencyCode = locale.currencyCode!"
  • "let price: Money<currencyCode.text> = 123.45" // Or something like this

Thoughts?

Thanks!

@mattt
Copy link
Member

mattt commented Jul 24, 2018

Hi @xcentric-solutions. Unfortunately, there's no mechanism that allows you to specify a dynamic currency type in the way you're describing. I discussed some of these limitations in this issue thread: Flight-School/Money#2

@xaepstudio
Copy link
Author

xaepstudio commented Jul 28, 2018

Thanks for the follow-up Matt. Unless I'm missing something, without such a dynamic mechanism, it appears the current Money implementation is flawed -- because it requires explicitly hard coding every value to a specific currency. If this is the case wouldn't it prohibit building a localizable app -- and instead require coding separate treatment for every desired locale?

I appreciate your objective of ensuring correctness of currency presentation with Money; but the mechanism employed appears to be a very expensive and likely unmaintainable approach for use cases where an app is conceptually universal (e.g., calculators). In those cases, potentially universal apps become limited solely due to the presentation mechanism.

This is obviously a hard problem and I appreciate your contribution; but wanted to try and explain the short-coming for your consideration. Thank you for Money and for considering my perhaps misguided suggestion.

@mattt
Copy link
Member

mattt commented Jul 29, 2018

@xcentric-solutions Thanks for your feedback.

I wouldn't go so far to say that this library flawed as much as it makes a conscious decision to require explicit handling of currencies. In some circumstances, it's totally reasonable to switch over the set of possible currencies in order to guarantee correct behavior.

But as I discussed in the thread I linked to, this guarantee may not be relevant for your particular use case, so you may decide that a non type-safe implementation is a better fit. For example, currency converters aren't performing transactions as their main function, so the developer might prefer something closer to this:

struct Money {
   enum Currency: String {
      case USD, CNY //...
   }

   var amount: Decimal
   var currency: Currency
}

This tradeoff is mentioned in the README under "Alternatives to Consider".

@mattt
Copy link
Member

mattt commented Aug 26, 2018

Closing this issue. Please let me know if you have any further questions or additional feedback, @xcentric-solutions. Thanks!

@mattt mattt closed this as completed Aug 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants