Why is #partial switch
required also with case:
?
#2342
-
When a Complete example, after a similar code from the documentation of package partial_switch
import "core:fmt"
Foo :: enum {
A,
B,
C,
}
main :: proc() {
f := Foo.A
// This works fine:
switch f {
case .A: fmt.println("A")
case .B: fmt.println("B")
case .C: fmt.println("C")
case: fmt.println("?")
}
// This works fine:
#partial switch f {
case .A: fmt.println("A")
case .C: fmt.println("C")
}
// Error:
// Unhandled switch case: B
// Suggestion: Was '#partial switch' wanted?
switch f {
case .A: fmt.println("A")
case .C: fmt.println("C")
case: fmt.println("?")
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The reason is that this is valid: You can create variants of an enum that are not backed by any named member. This value may come from a piece of runtime information that cannot be checked by the compiler. In other words, non-partial
package main
import "core:fmt"
Foo :: enum {
A,
B,
C,
}
test :: proc(foo: Foo) {
switch foo {
case .A: fmt.println("Named A")
case .B: fmt.println("Named B")
case .C: fmt.println("Named C")
case:
fmt.println("Unnamed", foo)
}
}
main :: proc() {
test(.A)
test(.B)
test(Foo(5))
}
|
Beta Was this translation helpful? Give feedback.
The reason is that this is valid:
f := Foo(5)
You can create variants of an enum that are not backed by any named member. This value may come from a piece of runtime information that cannot be checked by the compiler.
In other words, non-partial
switch
requires you to handlecase .Value