Skip to content

References to runtime values and attributes #209

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

Closed
eemeli opened this issue Nov 10, 2021 · 2 comments
Closed

References to runtime values and attributes #209

eemeli opened this issue Nov 10, 2021 · 2 comments
Labels
blocker-candidate The submitter thinks this might be a block for the next release data model Issues related to the Interchange Data Model functions Issue pertains to the default function set resolve-candidate This issue appears to have been answered or resolved, and may be closed soon. Stale Obsolete?

Comments

@eemeli
Copy link
Collaborator

eemeli commented Nov 10, 2021

Is your feature request related to a problem? Please describe.
With select messages, the case often arises that it's desirable for the selector value (in particular a number) to also be used within a select message, or for the selection to be made based on some attribute of a value that would then be used within the formatted message.

In MF1, the first of these is enabled by using # within a plural or selectordinal. In #52 (comment), @zbraniecki gives an example of the latter. This is also raised in #31 by @grhoten.

Describe the solution you'd like
We should determine how we can avoid error-prone duplication in MF2, so that e.g. numbers in select messages get formatted with the same options with which their cases were selected, and custom functions do not need to be called multiple times with the same arguments.

One option here is to e.g. include something like # in the MF2 data model, and to ensure that custom functions are pure, so that their results may be memoised for later calls with the same parameters.

Another option would be to introduce something like message-specific "macros" or "phrases" that have been suggested by @mihnita and @stasm in conversation elsewhere. The idea is to provide essentially a set of local variables that a message may use in its contents, with their values determined at runtime. This would be the solution that I'd prefer.

Describe why your solution should shape the standard
If such local variables are made available, they'll need a way to be defined in the syntax, as well as have a place in the data model. Using a made-up pseudosyntax, it might look something like this:

msg = [ $bar = number($foo, minimumSignificantDigits: 3) ] $bar ->
  [one] {$bar} thing
  [other] {$bar} things
{
  type: 'select',
  vars: {
    bar: {
      value: {
        type: 'function',
        func: 'number',
        args: [
          { type: 'variable', var_path: [{ type: 'literal', value: 'foo' }] }
        ],
        options: { minimumSignificantDigits: { type: 'literal', value: '3' } }
      }
    }
  },
  select: [
    { value: { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] } }
  ],
  cases: [
    {
      key: ['one'],
      value: [
        { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] },
        { type: 'literal', value: ' thing' }
      ]
    },
    {
      key: ['other'],
      value: [
        { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] },
        { type: 'literal', value: ' things' }
      ]
    }
  ]
}
@eemeli eemeli added syntax Issues related with syntax or ABNF data model Issues related to the Interchange Data Model labels Nov 10, 2021
@markusicu
Copy link
Member

From my proposed compromise syntax:

Inside selected patterns,
the selector argument variables must not be used with the normal $ placeholder syntax –
for example, the patterns in the preceding example must not use {$count}.
Allowing that would be doubly confusing:

  • It would not be clear which value is inserted.
    In the example, the plural offset is subtracted from the input value,
    and the formatted version of that is what is used for
    evaluating the plural rules and inserting into the pattern.
  • It would not be clear what formatting is applied.
    The formatting function and options specified in the selector must be used,
    but {$count} would look like the default formatter might be used.
    Allowing a function-and-options specification here would be even worse.
  • (If a developer does need a pattern with both the selector-modified and also the original value,
    then they can pass the value twice into the message formatting function,
    under different argument names.)

@romulocintra romulocintra removed the syntax Issues related with syntax or ABNF label Jul 18, 2022
@mihnita mihnita added the blocker-candidate The submitter thinks this might be a block for the next release label Nov 3, 2022
@aphillips aphillips added the functions Issue pertains to the default function set label Jul 16, 2023
@aphillips aphillips added Stale Obsolete? resolve-candidate This issue appears to have been answered or resolved, and may be closed soon. labels Dec 5, 2023
@aphillips
Copy link
Member

This appears to have been addressed? We now require selector expressions to have an annotation (either directly or in a declaration). Please reopen this issue or open new more-specific issues if you think otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker-candidate The submitter thinks this might be a block for the next release data model Issues related to the Interchange Data Model functions Issue pertains to the default function set resolve-candidate This issue appears to have been answered or resolved, and may be closed soon. Stale Obsolete?
Projects
None yet
Development

No branches or pull requests

5 participants