I wonder how dynamically typed languages behave when they found a matching case label. Will it continue evaluating the other label values that it hasn’t yet compared? (to find duplicate case labels or type errors).
How do common dynamically typed languages behave there?
I can only speak for JavaScript here, but JavaScript will search for a matching label by first checking that both operands are of the same type, then will check for value. If a match is found, the switch case will execute the statements associated with the labels and break out of the switch statement (if a break statement is found).
Example:
This will result in showing a popup saying
It's a string, Jim.. It’s actually not so different from a C-switch (sadly ;-)).EDIT
The
===operator is used here for comparison, which does not only test for value, but also for type. If the==operator would be used instead, the operands would only be tested for value (which would result in the first case being true).