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

Disable "smartness" of the compiler #10

Closed
matthiasfeist opened this issue Jan 21, 2016 · 4 comments
Closed

Disable "smartness" of the compiler #10

matthiasfeist opened this issue Jan 21, 2016 · 4 comments

Comments

@matthiasfeist
Copy link

If I have the following code in my project:

    var curr = 'USD';
    var currentcyFormatter = Globalize.currencyFormatter( curr );
    var cur = currentcyFormatter(123.123123);

I get the following error message curr is not defined
I think the compiler is trying to be a little bit too smart here and is trying to figure out which cldr data to include. But when I load stuff like this e.g. from a API, it can't know this just from the code.

So is there a option to disable this and to just tell the compiler maybe statically which data to include instead of trying to deduce it from the source?

@rxaviers
Copy link
Member

Actually, the goal of the compiler is to create runtime code for you. That means: fast and small code that doesn't require CLDR.

The compiler could be a little smarter and figure out that the curr variable is actually a constant and has a static value of "USD" and use that. But, note a variable could be "variable" (not a constant as in your example) and the solution could be undetermined at the compiler level/time. For example:

    var curr = fn(x, y, z);
    var currentcyFormatter = Globalize.currencyFormatter( curr );

Should we include all the code and data for all the available currencies? Including the whole code and data to allow any currency basically leads us back to step 0, where we are actually using Globalize normal modules, not the runtime modules. Note, this only gets worse if more and more variables are undetermined, for example:

    var currentcyFormatter = Globalize.currencyFormatter( currencyFn(x, y, z), optionsFn(a, b, c) );

Here we would have to include all the code and data for all the available currency formatters. For that reason, I suggest a different approach in your code:

var currentcyFormatter = Globalize.currencyFormatter( curr );

If you need multiple formatters, do this:

var currentcyFormatter = {
  USD: Globalize.currencyFormatter('USD'),
  EUR: Globalize.currencyFormatter('EUR'),
  ...
}

currentcyFormatter.USD(123.123123);

@rxaviers
Copy link
Member

I have closed this issue, but feel free to post new comments if you have any other question or suggestion. Thanks

@matthiasfeist
Copy link
Author

Thanks for the answer :)

@rxaviers
Copy link
Member

I'm reopening this issue so that we can brainstorm about potential ways to solve this. One is to allow the compiler to take an option with a list of supported currencies and use that list of currencies instead of the static currency (or probably a variable) it finds in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants