Is there a way to statically ensure an if-block covers all enum cases?

NHDaly
Fri, 08 Nov 2024 16:35:27 +0000
General Usage

Consider:

@enum Level LOW MEDIUM HIGH

function do_stuff(level::Level, args...)
    if level == HIGH
        println("High level")
    elseif level == MEDIUM
        println("Medium level")
    elseif level == LOW
        println("Low level")
    else
        @assert false "Unexpected Level: $level"
    end
    # ... do stuff...
end

Is there any way to statically (ideally at parse-time) enforce that the function is covering all the enum cases?

I think this basically needs to be a macro, like

@full_enum if level == HIGH
    ...
end

or something. Or maybe it needs to be a full blown switch syntax macro in order to ensure it’s parseable.

Does anyone know of a good one here?
Maybe one of the various pattern matching repos?

Thanks!

9 posts - 7 participants