-
-
Notifications
You must be signed in to change notification settings - Fork 36
Adding a :number offset option #926
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
Conversation
TODO: add tests. But I wanted to unblock the splitting of the registry in separate files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the option is available on :number
, it should also be on :integer
.
OTOH, if it only supports whole number values, it might make sense to only allow on :integer
; I'm not aware of any use case for it having been presented for non-integer values.
I also submitted #927 as the text around resolved value should be clarified.
The _resolved value_ of the _expression_ is determined by first subtracting the numeric value of `offset` from the _operand_, | ||
and then resolving the _expression_ on the calculated value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the offset is applied to the numerical value of the resolved value and the offset
option is also retained in the resolved options, doesn't that mean that it may get double-applied?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the trouble with using :integer (or :number) for both formatting and selection.
So selection you need both the offset, and the original value.
Because you test the exact values against the original value, and the keywords against the value - offset.
But for formatting the value - offset is enough, and you can drop the offset from the resolved options.
Which means that if one does
.input {$count :integer offset=2}
.match $count
...
then the resolved value for $count
should have the offset
.
If we separated the :input
/ :number
from :plural
this would have an easy answer: :plural
keeps the resolved value and resolved options "as is", and :integer
resolves the value to the value - offset and remove the offset from the resolved values.
I am not pushing to revert that decision.
We voted and all.
But I am open to suggestions, because I am not sure what to do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea: leave the resolved value as is, and leave the offset in the resolved options.
Then the selection can see both.
And the "formatted value" (which we don't have a concept of, although it would be useful) can do the diff.
Trouble is that without the concept of "formatted value" we can't fully describe how the formatting works.
We have a section named "Numeric Value Selection and Formatting", but we don't seem to describe the formatting part at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My view is that the :offset affects selection and formatting (in parallel), but it is up to us to define in the :number specification for offset
how successive :number offset values are combined — which does not prevent any other function options from combining in a different way.
Take the following:
.local $a {10 :number offset=2}
.local $b {$a :number offset=1}
.match $b
// please forgive any syntax errors
It is up to us to determine whether :number offset behaves like:
.local $a {10 :number :offset=3} // accumulates
or
.local $a {10 :number :offset=1} // replaces
I think we should have a general rule that unless otherwise stated, combinations of options do replacement, but that the specification for any given function option can override that behavior.
spec/registry.md
Outdated
- `offset` (optional) | ||
- ([digit size option](#digit-size-options), default: `0`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why a negative offset is not allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't imagine a use case for it.
ICU works. But also works with fractional values. And negative fractional values. And with fractional arguments (see my other comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose is to list the first n members of a list explicitly, then switch on the remaining number. Negative doesn't have any usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ICU compatibility sounds like a good argument to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partial review
@@ -449,14 +457,19 @@ Number selection has three modes: | |||
- `ordinal` selection matches the operand to explicit numeric keys exactly | |||
followed by an ordinal rule category if there is no explicit match | |||
|
|||
When the selection mode is `plural` implementations can optionaly support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't ordinal
work the same way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't ordinal work the same way?
I think we discussed it in the last meeting and nobody was able to come up with a use case for ordinals.
But ICU supports it.
So I am open to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list = { Audrey, Bill, Carlos, David, Edna, Freda}
.local $first = {$list :get item=1}
.local $second = {$list :get item=2}
.local $user = {$list :get item=last}
.local $length = {$list :length}
.local $position = {$length :integer select=ordinal offset=2}
.match $position
one {{After {$first} and {$second}, {$user} is {$position}st in line}}
two {{After {$first} and {$second}, {$user} is {$position}nd in line}}
... etc...
... but it's a pretty contrived use case...
Good catch, thank you. I added the option to About removing it from I can't imagine a use case for non-int with offset. BUT I have two reasons to keep it: One is that ICU supports it. You can pass a non-int argument and get "Alice, Bob, and 3.142 other guests". I cannot imagine a use case for any of these... The second reason I kept it was that I suspect that some devs would not be too careful about :number and :integer. But I am fine to say: no, we don't support any of this. Adding them later would be backward compatible. |
Co-authored-by: Addison Phillips <[email protected]>
Summarizing the questions:
|
Thinking about this some more, I think copying the MF1 plural offset option seems like the wrong thing to do: We are not restricted in the number of functions that are available, or the number of selectors. Considering the canonical example case for
I think one reason why the docs example doesn't use this formulation is that it showcases the weirdness of having the For MF2, yes, we could copy the structure directly, but that would retain the weirdness, and the reliance on looking up what actually happens with Wouldn't it be easier to read if we separated the behaviour into another function and used two variables? As in:
Would that not be much clearer about what each value is, what is being selected, and what is being formatted? The proposed |
@eemeli I had the same thought. Assigning a local integer and using the bog standard FWIW, since the primary use case is alleged to be working with lists, that suggests that the |
In that case as well my preference would be to not overload a |
I think it is a good ideal to handle the problem with a separate function. Moreover, it very much helps with clarity if we divide functions into two kinds:
|
Introducing non-selector/non-formatting functions does produce some complexity, since we require annotation in certain instances and implementations would have to check the type of function. BTW "different 'core' value" has a name: "resolved value". There are many obvious kinds of functions that could live here (cf. |
I used the term 'core value' specifically because it is not the same as the resolved value. .local $a = {123 :number style=decimal} These have different resolved values because they have different options. See https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md#:~:text=While%20this%20specification%20does%20not%20require%20it I agree with pulling offset= out of :number, because it makes the delineation between the different kinds of functions much cleaner. For a function that only selects or formats or both, composition can be very simple: override the options. That is, given:
The $y and $z would have exactly the same resolved value. That is then a very predictable and understandable model for users. It doesn't shut out having other kinds of functions, like :math, that have more complicated composition, eg in the following $y1 and $z1 would have the same resolved values, which is not just a simple "override the options" model.
|
I think "quite on the contrary" This is similar to the distinction between formatter functions and selector functions. |
Replacing with |
Fix for issue #701