April 04, 2023

On Friday, 31 March 2023 at 15:20:41 UTC, Steven Schveighoffer wrote:

>

Why not? How much does this rule help you, vs. annoy you?

[Little OT] I don’t really care. In my experience, a switch is one of the two cases: Every case leads to a return or every case leads to a break. Occasionally, there are cases ending in throw. When I code C#, which I do occasionally, I almost never use switch statements, but switch expressions¹, where every code path necessarily must return a value. The best part about C#’s switch expressions is that they don’t need (or even allow for) break. I consider switch statements like goto: It’s there when you need it, but it shouldn’t be your first attempt.

As D implemented DIP 1043 Shortened Method Syntax,² it seems reasonable to also implement switch expressions in D much alike C#’s.

¹ https://learn.microsoft.com/en-US/dotnet/csharp/language-reference/operators/switch-expression
² https://github.com/dlang/DIPs/tree/master/DIPs

April 15, 2023

On Tuesday, 4 April 2023 at 12:47:18 UTC, Quirin Schroll wrote:

>

I don’t really care. In my experience, a switch is one of the two cases: Every case leads to a return or every case leads to a break. Occasionally, there are cases ending in throw. When I code C#, which I do occasionally, I almost never use switch statements, but switch
expressions¹, where every code path necessarily must return a value. The best part about C#’s switch expressions is that they don’t need (or even allow for) break. I consider switch statements like goto: It’s there when you need it, but it shouldn’t be your first attempt.

Although I like the switch, the use of a look-up table increases the assembly code when operating within a certain range (chain of possibilities). For example, the first code snippet is more efficient:

import core.stdc.stdio: printf;

void main() {
 char letter = 63;
 while(++letter) {
   //* <- remove slash for toggle-snippet2 on:
   if(letter > 64 && letter < 91) {
     printf("capital: %c\n", letter);
   } else if(letter > 96 && letter < 123) {
     printf("small: %c\n", letter);
   }/*/
  switch(letter) {
    case 'A': .. case 'Z':
      printf("capital: %c\n", letter);
      break;
    case 'a': .. case 'z':
      printf("small: %c\n", letter);
      break;
    default:
   }//*/
 }
}

I also like the second snippet, but it is necessary to use it carefully. Even if you compile with -O2, a rodata segment (.rodata) is generated unnecessarily.

https://d.godbolt.org/z/1q5YoPosT

Going back to our topic, don't take away the default! Because we need it in this example.

SDB@79

April 18, 2023

On Saturday, 1 April 2023 at 07:32:28 UTC, claptrap wrote:

>

How often does anyone use "default: break"?

I do that all the time. I don't assert the default statement, because in a lot of my projects I don't want to handle all cases always.

1 2 3 4
Next ›   Last »