Reason
Quick StartTryGuideAPICommunityBlogGitHub
Search

If-ElseSuggest an edit

if (showMenu) {
  displayMenu ();
};

Reason ifs are expressions; they're evaluated to their body's content:

let message = if (isMorning) {
  "Good morning!"
} else {
  "Hello!"
};

We also have ternary sugar.

let message = isMorning ? "Good morning!" : "Hello!";

Usage

if-else and ternary are much less used in Reason than in other languages; Pattern-matching kills a whole category of code that previously required conditionals. Prefer if-else if you only have, say, 2 branches.

Design Decisions

Reason ternary is just a sugar for the bool variant and a switch:

switch isMorning {
| true => "Good morning!"
| false => "Hello!"
}

If you pass that through refmt, you'd get:

isMorning ? "Good morning!" : "Hello!";

Interested? Here's a blog post about the spirit of our refmt.