You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support dynamic if-cast with multiple is type checks.
Use Case
The following would cause: error: field 'commonfield' does not exist or have the same type in all sumtype variants, despite it does exist on the two if is-checked variants.
This is also the case without additional conditions, i.e. if s is S1 || s is S2. b should just illustrate anything additiona that would need to be taken care of in a solution.
Proposed Solution
If there are multiple is in an if condition (i.e. split by a logical or) they could resemble multiple if conditions under the hood.
E.g. this:
if b && (s isS1|| s isS2) {
println(s.commonfield)
}
Would become this:
if b && s isS1 {
println(s.commonfield)
}
if b && s isS2 {
println(s.commonfield)
}
or
if b {
if s isS1 {
println(s.commonfield)
}
if s isS2 {
println(s.commonfield)
}
}
I'm not having looked into how if-is cast are handled so there might be even better approaches.
Other Information
Currently it would be possible to use a coma separated match arm for this which would generate a similar code.
match s {
S1, S2 {
if b {
println(s.commonfield)
}
}
else {}
}
Acknowledgements
I may be able to implement this feature request
This feature might incur a breaking change
Version used
0.3.4
Environment details (OS name and version, etc.)
linux,arch;amd64
edit(2023.06.09): fix typos
edit(2023.06.14): update example for match equivalent, improve problem description
This discussion was converted from issue #18393 on June 27, 2023 10:02.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Describe the feature
Support dynamic if-cast with multiple
is
type checks.Use Case
The following would cause:
error: field 'commonfield' does not exist or have the same type in all sumtype variants
, despite it does exist on the twoif is
-checked variants.This is also the case without additional conditions, i.e.
if s is S1 || s is S2
.b
should just illustrate anything additiona that would need to be taken care of in a solution.Proposed Solution
If there are multiple
is
in an if condition (i.e. split by a logical or) they could resemble multiple if conditions under the hood.E.g. this:
Would become this:
or
I'm not having looked into how if-is cast are handled so there might be even better approaches.
Other Information
Currently it would be possible to use a coma separated match arm for this which would generate a similar code.
Acknowledgements
Version used
0.3.4
Environment details (OS name and version, etc.)
linux,arch;amd64
edit(2023.06.09): fix typos
edit(2023.06.14): update example for match equivalent, improve problem description
Beta Was this translation helpful? Give feedback.
All reactions