C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A draft for modern switch

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 21 May 2025 10:15:56 +0200
With a new keyword there are more possibilities to create the optimal syntax and correct some of the shortcomings of switch case like fallthrough.   Have you looked at the first link I provided, those all have similar syntax to yours for matching strings? If a new syntax and facility is created anyway, why make any changes to switch?   -----Ursprüngliche Nachricht----- Von:Filip <fph2137_at_[hidden]> Gesendet:Mi 21.05.2025 10:19 Betreff:Re: [std-proposals] A draft for modern switch An:std-proposals_at_[hidden]; CC:Sebastian Wittmeier <wittmeier_at_[hidden]>; std-proposals_at_[hidden]; To me int is not special here, compiler can optimize it since it knows how to compare it. So maybe using constexpr operator?  Why not have something like switch constexpr for all types that have constexpr operator ==, Usual switch would not do that by default for anything other than int like it does now.  Pattern matching would be possible in switch with minimal additional syntax change with something like:  switch (a) {   case < 42: …   case 50 … 80: …   case [](auto a_) { return a_ < 88; } : … }  Operator < should then also be constexpr.  It is syntactic sugar, but it’s not impossible to change it. What essentially is there in the case is a function that immediately compares value of the switch. Cheers, Filip Wiadomość napisana przez Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> w dniu 21 maj 2025, o godz. 09:59:  First what is wrong with the existing suggestions? Here you have a comparison of three proposals: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2688r5.html#matching-strings   Many languages call their "switch" differently.   You still have not specified, whether you suggest that the compiler can assume  - the given string is one of the options or  - the given string can be any string  - the given string can be any string only if a default case is provided   Depending on it, those shorter hash solutions or comparing the first few characters would work or not.   Normally switch is a simple function / logic. If it now could compare more complex objects, this should be clearly defined.   All other programming languages allowing switch (or which name they chose) to do string comparisons, do a full (non-optimized to the selection) comparison, as if you do a chain of if/else.   If this is what you also propose, then perhaps you should extend it to any custom type, too. Why should strings be something special?   MyObject o(42); ... switch (o) { case o2:     ...     break; case MyObject(3):     ...     break; case MyObject(5):     ...     break; default:     ... }   -----Ursprüngliche Nachricht----- Von:Zhao YunShan via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 21.05.2025 04:15 Betreff:Re: [std-proposals] A draft for modern switch An:Nikolay Mihaylov <nmmm_at_[hidden]>; CC:Zhao YunShan <dou1984_at_[hidden]>; std-proposals_at_[hidden];   switch is a concept familiar to all programmers. Most languages use switch with strings, including JS, Golang, PHP, etc. match seems a bit unusual in comparison.   Indeed, using a constexpr hash function can construct a perfect hash, but my idea is to have the compiler handle this task rather than doing it manually myself.     Regards    -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-05-21 08:23:27