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

Add on directive to switch as an alternative to case #114

Merged
merged 3 commits into from
Aug 28, 2024

Conversation

scrhartley
Copy link

This is a copy of #106 which was backed out after a problem with the contributors agreement.


The current switch directive is not recommended due to its fall-through behavior being regarded as error-prone. To solve this problem, we introduce a new on directive as an alternative to case, which doesn't support fall-through. This new directive allows specifying multiple comma-separated conditions in order to address the primary motivator for using fall-through with case.

Example

Using case:

<#switch animal.size>
  <#case "tiny">
  <#case "small">
     This will be processed if it is small
     <#break>
  <#case "medium">
     This will be processed if it is medium
     <#break>
  <#default>
     This will be processed if it is neither
</#switch>

Using on:

<#switch animal.size>
  <#on "tiny", "small">
     This will be processed if it is small
  <#on "medium">
     This will be processed if it is medium
  <#default>
     This will be processed if it is neither
</#switch>

Details

  • Mixing case and on directives is disallowed and will fail.
  • If a switch contains an on, then using break or continue is not supported (applying to both on and default). In this situation, break and continue will follow the behavior of the containing scope, e.g. of a containing list. This does mean that if someone accidentally uses break with on, they could potentially become confused.

Additional Notes

  • When on is not used, the legacy behavior for case/default is that continue will be treated as break. If we wish to change this, then that work can be done separately.
  • If using default, the parser also allows it to appear before or between case directives, but when using on then default is only allowed after.

@ddekany ddekany merged commit 7a429fe into apache:2.3-gae Aug 28, 2024
2 checks passed
@ddekany
Copy link
Contributor

ddekany commented Aug 28, 2024

Thanks, was merged. Also I have applied exactly the same changes as after PR #106, just have put all 3 commits into 1 here.

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.

2 participants