Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't julia support something like switch-case

Tags:

julia

Julia doesn't support something like a switch-case a control structure, at least according to the current documentation of Control Flow?

switch case is a common Flow Control in imperative or object oriented languages, why not in julia?

Language supporting switch-case (not completed)

  • C/C++
  • Java
  • Pascal
  • PHP
  • Javascript
  • Typescript
  • Octave
like image 499
stefan bachert Avatar asked Aug 10 '17 14:08

stefan bachert


People also ask

Does Julia have switch statement?

Julia lacks a C-style switch statement. This issue has come up before on various fora. Unsurprisingly, Julia also lacks pattern matching, a useful generalization of case and switch, which has been implemented as a macro for Julia.

What is the purpose of Julia?

Julia is designed for parallelism, and provides built-in primitives for parallel computing at every level: instruction level parallelism, multi-threading, GPU computing, and distributed computing. The Celeste.jl project achieved 1.5 PetaFLOP/s on the Cori supercomputer at NERSC using 650,000 cores.

Is switch better than if else Java?

Speed: A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.


1 Answers

The basic philosophy of julia is to provide most functionality as packages and keep the core (Base) ultra-lean. So the answer to "why doesn't Julia support X" is usually "Julia supports X via package Y". In this case, the Match.jl provides a switch-case like structure that is very powerful. There is also a Switch.jl package that is very close to C's switch, but it is not actively maintained.

like image 68
Michael K. Borregaard Avatar answered Sep 29 '22 15:09

Michael K. Borregaard