fsharp / fsharp/fslang-suggestions

Add if! (if-bang) keyword to computation expressions

Open
#863 21 comments 24 reactions 0 assignees View on GitHub
approved-in-principle area: computation-expressions
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Add if! (if-bang) keyword to computation expressions
I propose we extend computation expressions with a new keyword, `if!` (if-bang), that functions a ``let!`` (let-bang) combined with an if statement. For example, instead of writing:

```fsharp
let! shouldSolveWorldHunger = confirmAsync ("Solve world hunger?")
if shouldSolveWorldHunger then solveWorldHunger () else dont ()
```

or even:

```fsharp
match! confirmAsync("Solve world hunger?") with
| true -> solveWorldHunger ()
| false -> dont ()
```

We should be able to do:

```fsharp
if! confirmAsync("Solve world hunger?") then
solveWorldHunger ()
else dont ()
```

## Pros and Cons

Pros: Aids code readability in specialized cases.

Cons: It's work, and it increases complexity. (whether or not this is worth the effort for potentially such small return is up to debate / whoever would do it, but I figure it's worth filing [probably low priority :) ] since I am not aware of a prior suggestion -- would be brownie points for the F# language)

## Extra information

S

Related suggestions: (put links to related suggestions here)

#572

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.