• smiletolerantly@awful.systems
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    Drop if-let

    Over my cold dead body. if-let-else is such a fantastic pattern. Makes everything an order of magnitude more readable. Works so nicely for unwrapping opts/errs.

    • TehPers@beehaw.org
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      Their suggested replacement is closer to C#'s is keyword:

      if foo is Some(foo) {
          // ...
      }
      

      The issue here is that we still have let-else, which can’t be translated as easily:

      let Some(foo) = foo else {
          todo!();
      };
      
        • TehPers@beehaw.org
          link
          fedilink
          English
          arrow-up
          0
          ·
          1 year ago

          That’s not an alternative, it’s removing let-else entirely. It’s equivalent to this:

          let i = if let Some(i) = opt_number {
              i
          } else {
              return 0;
          };
          

          let-else is specifically a feature that allows you to use a refutable pattern to deconstruct a value by providing a diverging else branch.