Skip to content

Why is #partial switch required also with case:? #2342

Answered by z64
programandala-net asked this question in Q&A
Discussion options

You must be logged in to vote

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 handle

  • All named values must have an explicit case .Value
  • A default codepath for unnamed values
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

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by programandala-net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants