DEV Community

Alex Verein
Alex Verein

Posted on

Comparing switch-like operators in 14 different programming languages

We are going to look at Javascript, Typescript, Java, C#, Swift, Objective-C, Python, PHP, Ruby, Kotlin, ReasonML, Dart, Go and Elixir.

At Avo ( we work with 12 languages, and I decided to do a series of quick comparisons of those languages, plus 3 languages we are going to support soon.

First feature we are going to look at is the switch-like constructs. We are interested in 3 features related to switches - ability to use switch as an expression (so it returns a value), whether the switch construct is exhaustive (you need to handle all cases to compile the code) and if the fall-through logic disabled (so you don't need to break out of each case explicitly).

Language has switch expression is exhaustive no need to break reference
javascript limited, only if wrapped in a function - -
typescript limited, only if wrapped in a function limited - same as JS
java starting java 12 expression is, statement is not additional syntax starting java 12
c# starting C# 8.0 - (expression will give a compile warning) need to break in the statement
swift - + +
objc - + - 🤷‍♂️
python no switch in python :)
php called match, introduced in php 8 match is, switch is not match , switch is not
ruby, called case + - +
kotlin, called when + expression is, statement is not +
reasonml/rescript + + +
dart - + -
go - - +
elixir (called case) - - (runtime error CaseClauseError) +

Discussion (0)