Be able to select all unhandled values in a discriminated union switch #5680
Closed
godalming123
started this conversation in
Ideas/Requests
Replies: 3 comments
-
|
It already works like this =b https://godbolt.org/z/9f8Tv8T4s package main; import "core:fmt"
main :: proc() {
foo("bar") // outputs "String: bar"
foo(3) // outputs "Int: 3"
foo(f32(3.4)) // outputs "Unknown 3.4"
foo(nil) // outputs "Nil"
}
foo :: proc(value: union{int, f64, f32, string, []string}) {
#partial switch val in value {
case string:
fmt.printfln("String: %s", val)
case int:
fmt.printfln("Int: %d", val)
case nil:
fmt.println("Nil")
case:
fmt.println("Unknown", val)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Yo can already do what you want. foo :: proc(value: union{int, f64, f32, string, []string}) {
#partial switch val in value {
case string:
fmt.printfln("String: %w", val)
case int:
fmt.printfln("Int: %d", val)
case nil:
fmt.println("Nil")
case:
fmt.printfln("Unknown: %v", val)
}
}
main :: proc() {
foo("bar") // String: "bar"
foo(3) // Int: 3
foo(f64(3.4)) // Unknown: 3.39999999999
foo(nil) // Nil
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Something like:
Beta Was this translation helpful? Give feedback.
All reactions