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

Return Iterable<MessagePart> from toParts #23

Open
stasm opened this issue Aug 18, 2023 · 1 comment
Open

Return Iterable<MessagePart> from toParts #23

stasm opened this issue Aug 18, 2023 · 1 comment

Comments

@stasm
Copy link
Contributor

stasm commented Aug 18, 2023

If possible in the language of the spec, consider returning an Iterable<MessagePart>? Conforming implementations can still return an array of parts, but some use-cases may benefit from a more lazy approach made possible by the use of generators, for example.

Originally posted by @stasm in #22 (comment)

@eemeli
Copy link
Member

eemeli commented Aug 18, 2023

Collecting some of my comments on this here from the original thread, so that it may be closed:


Would that make the error handling potentially weird, though? Let's say that a user does this:

const src = '{n is {$n :number minimumFractionDigits=999}}';
const mf = new Intl.MessageFormat(src, 'en');
const parts = mf.formatToParts({ n: 42 }, console.warn);
for (let part of parts) console.log(part);

With array output, it's pretty clear that the console.warn call will happen before the console.log calls. But with an iterable that's formatting parts lazily, that might log:

{ type: 'literal', value: 'n is ' }
RangeError: invalid digits value: 999
{ type: 'fallback', source: '$n' }

whereas a more eager implementation would report the error earlier.


I think we do want an array. Not because we may want to read its length or access parts by index, but because:

  1. It's clearer to the caller that onError will never be called after the function has returned a value.
  2. It's much more user-friendly to allow resetting the position within the parts.

@stasm It would be helpful to have an example or two of use cases which "may benefit from a more lazy approach made possible by the use of generators".

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