Skip to content

Add variable resolution #305

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

Merged
merged 1 commit into from
Oct 24, 2022
Merged

Conversation

eemeli
Copy link
Collaborator

@eemeli eemeli commented Oct 17, 2022

Builds on #304, which should be merged before this.

Closes #297

This PR does not attempt to provide exhaustive contents for variable resolution, but at least a start that should correspond to the consensus reached (so far) in issue #297. It also clarifies the precedence of local vs. external variables, on which we've had some discussion, but I'm not able to find a corresponding issue.

CC @macchiati, who I wasn't able to add directly as a reviewer.

If a local variable and an externally defined one use the same name,
the local variable takes precedence.

It is an error for a local variable definition to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The language is a bit awkward; I suggest:

Any reference to a local variable before it is defined is an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@macchiati Good point; replaced with your suggestion.

eemeli added a commit to messageformat/messageformat that referenced this pull request Oct 17, 2022
@stasm
Copy link
Collaborator

stasm commented Oct 17, 2022

Perhaps we should discuss and try to resolve #248 at the plenary meeting today. It would make it easier to make progress on the formatting spec.

@eemeli eemeli force-pushed the add-variable-resolution branch from 59cbfd7 to 8630218 Compare October 17, 2022 17:56
its Name is used to identify either a local variable,
or a variable defined elsewhere.
If a local variable and an externally defined one use the same name,
the local variable takes precedence.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies that name shadowing is possible. My current thinking is that it should be OK, and I see use-cases for it. However, I don't recall an explicit discussion on the subject. It might also make sense to start with a stricter design and disallow shadowing at first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather think that local variables must be allowed to shadow externally provided named arguments.

For instance, I could well imagine translation systems with the following attributes:

  1. Translators are able to define additional local variables for a message.
  2. A developer may pass the same basket of arguments to multiple messages, not all of which use each of them.

If shadowing were not allowed, then a combination of the above could result in a runtime error which would be difficult to discover ahead of time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this part of the change (shadowing) is a bit of an overreach.
It is unrelated to the hoisting problem, and it was not commonly agreed.

In general I don't think it is a good idea to allow translators to override a developer decision.
So "Translators are able to define additional local variables for a message." is a cons in my book, not a pro.

And "A developer may pass the same basket of arguments to multiple messages, not all of which use each of them." does not look like something related to shadowing.

There is a reason many linters warn about shadowing.

"If shadowing were not allowed, then a combination of the above could result in a runtime error which would be difficult to discover ahead of time."

Not necessarily.
One can say that translators can't define variables (and that is a good thing).
It is an error, but the decision between runtime error and simply ignore the conflicting local variable is implementation dependent.
We should not use that as an argument for what the standard should do.


But TLDR:

  • I don't see valid use-cases for it. In fact the proposal we had used different sigils for local variables (macros at the time) and arguments.
  • I don't recall an explicit discussion on the subject.
  • It might also make sense to start with a stricter design and disallow shadowing at first.

Filed #310

@echeran echeran merged commit c5eb07e into unicode-org:main Oct 24, 2022
@eemeli eemeli deleted the add-variable-resolution branch October 24, 2022 17:06
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

Successfully merging this pull request may close these issues.

Hoisting local variables or not
5 participants