Comment

Comment by jlebar

My favorite (not actually horrible) switch statement trick:

Instead of

  string x;
  switch (y) {
    case 0: x = "foo"; break;
    case 1: x = "bar"; break;
  }
try an IIFE!

  string x = [&] {
    case(y) {
      case 0: return "foo";
      case 1: return "bar";
    }
  }();
Once you get used to reading it, there are a bunch of advantages. Among them, you can't forget a "break", and you can't forget to assign into `x`.
parent jlebar 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton