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

Support for a switch operation #819

Closed
SimonCockx opened this issue Sep 2, 2024 · 4 comments · Fixed by #829
Closed

Support for a switch operation #819

SimonCockx opened this issue Sep 2, 2024 · 4 comments · Fixed by #829
Assignees
Labels
enhancement New feature or request subject: code generation This issue is about code generation subject: model validation This issue is about validation of Rosetta models, such as the type system subject: syntax This issue is about the syntax of Rosetta

Comments

@SimonCockx
Copy link
Contributor

SimonCockx commented Sep 2, 2024

It is often useful to model a case analysis based on an enum, string or number, e.g.,

if input = MyEnum -> VALUE1
then ...
else if input = MyEnum -> VALUE2
then ...
...
else ...

It would be useful to have a shorthand operation for this, e.g.,

input switch
    VALUE1 then ...,
    VALUE2 then ...,
    ...
    default ...

In case of switching over an enum value, Rune would validate that all enum values have been exhausted, otherwise indicating to the modeller that they forgot a case. As a catch-all, the default keyword can optionally be used at the end. This validation would improve consistency while refactoring, as new enum values will automatically indicate to the modeller which expressions should be updated.

In case of switching over basic types such as strings or numbers, no validation would be present, and a default empty is assumed implicitly.

Implementation consequences

The Rune language adds a new expression to its syntax. Code generators supporting expressions (Java, Python) will need to implement its semantics.

@SimonCockx SimonCockx added enhancement New feature or request subject: code generation This issue is about code generation subject: syntax This issue is about the syntax of Rosetta subject: model validation This issue is about validation of Rosetta models, such as the type system labels Sep 2, 2024
@lolabeis
Copy link
Contributor

lolabeis commented Sep 2, 2024

Looks good. And it would be the same syntax if switching on a choice type (i.e. implementing no. 3 in #797)?

@SimonCockx
Copy link
Contributor Author

SimonCockx commented Sep 2, 2024

Indeed, the same syntax could be used to switch over choice types as well in the future.

@manel-martos
Copy link

I think this is a great addition into the DSL expressions. As it was already commented during the Technical Architecture Working Group, the break is assumed which avoids modelers to apply the same expression to multiple cases without code replicability. Languanges such as Java allow it so, how will DSL represent such cases?

e.g.
input switch VALUEA, VALUEB then runAB, VALUEC then runC, default ...

@SimonCockx
Copy link
Contributor Author

@manel-martos Good question. Right now, the expression will have to be repeated, e.g.,

input switch
    VALUEA then exprAB,
    VALUEB then exprAB,
    VALUEC then exprC

This is the case in other programming languages such as Haskell as well. However, if this pattern often arises in the CDM or other models, then I think it would indeed be valuable to have such a feature, e.g.,

input switch
    VALUEA, VALUEB then exprAB,
    VALUEC then exprC

Feel free to raise a separate feature request so we can keep track of the discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request subject: code generation This issue is about code generation subject: model validation This issue is about validation of Rosetta models, such as the type system subject: syntax This issue is about the syntax of Rosetta
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants