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

Specifiy allowed or forbidden behavior of Functions #294

Open
Pike opened this issue Sep 17, 2019 · 3 comments
Open

Specifiy allowed or forbidden behavior of Functions #294

Pike opened this issue Sep 17, 2019 · 3 comments
Labels

Comments

@Pike
Copy link
Contributor

Pike commented Sep 17, 2019

I think we should make some statements around what Functions are allowed to do and not, as part of our resolver spec.

In particular, I think we should

  • define functions to be deterministic
  • forbid functions to have side effects
@SirNickolas
Copy link

I had to do some work to account for nondeterministic/impure functions, so forbiding them will simplify things a bit. But aren’t there use cases which do require them? How would you write this, for example? (Different localizations may define different number of synonyms.)

greeting = { RANDOM(3) ->
    [1] Greetings, {$userName}!
    [2] Hello, {$userName}!
   *[3] Hi, {$userName}, and have a nice day!
}

@spookylukey
Copy link
Contributor

spookylukey commented Sep 17, 2019

I'd prefer to see functions defined as being deterministic with no side effects - because of the principle of least power, and considerations for the different implementations I'm working on, several of which assume pure functions only :-)

I can see the use case for functions like RANDOM, but also lots of problems. For example, for some implementations you are going to have a very hard time implementing RANDOM at all - for example elm-fluent which works by compiling to Elm, which is a pure functional language. So if you have usages of RANDOM in your FTL messages, this just isn't going to work if you try to use these messages via elm-fluent or any similar implementation, and so it would probably be a bad idea to encourage use of that kind of function.

I can think of one work-around for implementing greeting above:

greeting = { $alternative ->
    [1] Greetings, {$userName}!
    [2] Hello, {$userName}!
   *[3] Hi, {$userName}, and have a nice day!
 }
         .alternative-count = 3

The application would have to retrieve the greeting.alternative-count value, convert that to an integer, then generate an appropriate random number and pass that to the greeting message as the $alternative argument.

So

  • you could only do random numbers with the cooperation of the application code. Given that random number generation really is a side effect, and we do not expect translation messages to be able to do any other side effects like writing to DB etc., this seems reasonable.
  • it's not very pretty, but if you don't need it that often it's perhaps not too bad.

@SirNickolas
Copy link

SirNickolas commented Sep 17, 2019

Thank you, your solution looks nice (well, nicer than those I thought about). And if a message needs several random numbers (don’t know why), it can request a bigger number and call DIV/MOD to split it up.

And… aside from RANDOM, I can’t think of another impure function that would be useful to a translator.

SirNickolas added a commit to SirNickolas/fluentd that referenced this issue Oct 8, 2019
They are seldom needed to localizers and aren't portable across
implementations. See related discussion:
projectfluent/fluent#294
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants