# 'if let' expression

**URL:** <https://discourse.elm-lang.org/t/if-let-expression/7310>\
**Category:** Request Feedback\
**Created:** [April 30, 2021, 6:34pm UTC](https://discourse.elm-lang.org/t/if-let-expression/7310 "2021-04-30T18:34:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronanyeah](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ronanyeah/32/5497_2.png) [@ronanyeah](https://discourse.elm-lang.org/u/ronanyeah)\
**Post date:** [April 30, 2021, 6:34pm UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/1 "2021-04-30T18:34:08Z")

</div>

Has the idea of an ‘if let’ expression, like in Rust, ever been discussed for Elm?  
[https://doc.rust-lang.org/rust-by-example/flow\_control/if\_let.html](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html)

It could look like this:

```auto
type Num
    = None
    | Zero
    | Num Int

greaterThanFive : Num -> Bool
greaterThanFive val =
    if let Num n = val then
        n > 5

    else
        False

```

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [April 30, 2021, 7:02pm UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/2 "2021-04-30T19:02:23Z")

</div>

I don’t think it has been discussed before. That said, do you gain anything from writing it that way instead of like this? It’s only one line longer.

```elm
type Num
    = None
    | Zero
    | Num Int

greaterThanFive : Num -> Bool
greaterThanFive val =
    case val of 
        Num n ->
            n > 5

        _ -> 
             False
```

---

<div class="post-metadata">

**Author:** ![mattpiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattpiz/32/860_2.png) [@mattpiz](https://discourse.elm-lang.org/u/mattpiz)\
**Post date:** [April 30, 2021, 9:33pm UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/3 "2021-04-30T21:33:07Z")

</div>

I don’t think there is an advantage in having this syntax in elm because we are forced to have an `else` branch to have total functions. It’s thus roughly the same than doing pattern matching. This is not the case in Rust where you can have early returns, where this syntax is mostly useful.

---

<div class="post-metadata">

**Author:** ![ronanyeah](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ronanyeah/32/5497_2.png) [@ronanyeah](https://discourse.elm-lang.org/u/ronanyeah)\
**Post date:** [April 30, 2021, 11:27pm UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/4 "2021-04-30T23:27:24Z")

</div>

Yeah true, I mostly use it for early returns.

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [May 1, 2021, 8:21am UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/5 "2021-05-01T08:21:25Z")

</div>

Also note that `if let` is already valid syntax, though it doesn’t do what’s proposed in this thread.

```elm
if let sum = 1 + 2 in sum == 3 then
    "Math works!"
else
    "I don’t even…"

```

---

<div class="post-metadata">

**Author:** ![jessta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jessta/32/660_2.png) [@jessta](https://discourse.elm-lang.org/u/jessta)\
**Post date:** [May 2, 2021, 6:25am UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/6 "2021-05-02T06:25:26Z")

</div>

I think it would be strange to add syntax specifically to make it easier/nicer to do something that is generally discouraged.  
This syntax is just a nicer way of doing:

```
  greaterThanFive : Num -> Bool
  greaterThanFive val =
      case val of 
          Num n ->
              n > 5

          _ -> 
               False

```

But generally it’s discouraged to write functions that don’t handle all variants of a type. So it would be strange to have special syntax for such a rare case.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 12, 2021, 6:26am UTC](https://discourse.elm-lang.org/t/if-let-expression/7310/7 "2021-05-12T06:26:11Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
