Is there a "select case" equivalent in Julia?

I purposefully didn’t use ternaries because the original Fortran example had cases with multiple statements. Sure you could use begin ... end there, but it erases any cleanliness benefits.

I haven’t verified whether your snippet or any of the ones below generate a switch, but I don’t have to either! Anyone can easily check for one just by using @code_llvm. To confirm an actual jump table is being generated (which, again, I could get neither the Julia compiler or GFortran to do), simply use @code_native and check for this this kind of pattern.

I am fine with chained ternaries, was just suggesting another option.

Generally, a C-like switch is a very special case of control flow that can be handled with generic constructs, and not offering specialized syntax may be the right call. There are multiple solutions, so it is up to personal preference to pick one.

2 Likes