[FEATURE] Simple switch statement #295
Replies: 7 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Someone on the discord proposed that a trivial match expression could be transpiled into a C switch statement. I think this would be an elegant solution. |
Beta Was this translation helpful? Give feedback.
-
Switch is generally optimized pretty well either with sorted prongs or a jump table (for example). If-else is optimized in some cases but I am not sure of what would drive that. All this in modern C compilers: GCC, Clang, and MSVC. There really are not any others that matter that much for the vast majority of platforms. There are a few specialized embedded ones but those are dying out too as GCC can produce code for almost every embedded CPU used these days. In your example, if the type is a value in the object/class in the transpiled C code then it could be turned into a switch-case on translation. Not saying it would be easy but I think that is a much better situation than having two similar constructs that overlap in function. Everything the proposed switch can do, match can do. My advice is to not attempt to optimize Zen-C at this point. Fix the feature set. Get it to work first. Optimize later. IMHO it is already starting to get a collection of features that probably do not interact all that well (only judging from history after watching C++ start nice and end up a complete Frankenstein's monster of a language). |
Beta Was this translation helpful? Give feedback.
-
|
As much as I hate to post links to Reddit: Comparing switch-case to if-else. It is extremely situation dependent which is faster. So, please, just keep match and do not add anything else unless you can prove that there are common cases where adding switch/case would be a clear win. |
Beta Was this translation helpful? Give feedback.
-
|
Even a small change in how the CPU does branch prediction can completely skew the results. In 99.99% of the cases, clarity of code is far more important than the performance of low level constructs like this. |
Beta Was this translation helpful? Give feedback.
-
|
This makes more sense as a discussion for now. |
Beta Was this translation helpful? Give feedback.
-
|
Now that we have implemented |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
matchexpression in Zen-C are more expressive thanswitchexpression in plain C. But they get transpiled into a sequence ofif-elsestatements. There is currently no way in Zen-C to generate a regularswitchstatement without using arawblock and write it in plain C. Which can be an issue depending on the C compiler as aswitchstatement is more likely to be optimized over anif-elsesequence.Add a
switchstatement (orraw_match) which would transpile trivially to a Cswitchstatement. Thatswitchstatement would only support primitive types and enum variants and it would not support ranges. Contrary to Cswitch, it would not be necessary to addbreakafter each case but instead there would be afallthroughkeyword indicating that a given case can fallthrough the next one defined.So something like this
Would transpile into this
Beta Was this translation helpful? Give feedback.
All reactions