Do you think if statement as expression would be nice to have in D?
SealabJaster
sealabjaster at gmail.com
Sat Jun 4 10:01:31 UTC 2022
On Saturday, 4 June 2022 at 09:56:52 UTC, Walter Bright wrote:
> D has:
>
> auto r = (n > limit) ? "big" : "small";
>
> which is more readable than Rust's :-)
While I do agree that's more readable, I wonder if the OP was
thinking more along the lines of this, which is what you can see
in languages like F# where if statements are expressions:
```
auto number = 200;
auto myVar = if (type == "as-is") {
return number;
} else {
// Very complex algorithm
number *= 2;
number += 2;
number /= 2;
number = number > 300 ? 200 : 100;
return number;
}
```
F# takes it a step further since pretty much everything is an
expression, so you can even use pattern matching as an expression
:D https://fsharpforfunandprofit.com/posts/match-expression/
More information about the Digitalmars-d
mailing list