Do you think if statement as expression would be nice to have in D?
Dukc
ajieskola at gmail.com
Sat Jun 4 19:54:48 UTC 2022
On Saturday, 4 June 2022 at 10:01:31 UTC, SealabJaster wrote:
> 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;
> }
> ```
```D
auto number = 200;
auto myVar = type == "as-is"?
number:
{ number *= 2;
number += 2;
number /= 2;
number = number > 300 ? 200 : 100;
return number;
}();
```
More information about the Digitalmars-d
mailing list