Do you think if statement as expression would be nice to have in D?
user1234
user1234 at 12.de
Sat Jun 4 11:35:41 UTC 2022
On Saturday, 4 June 2022 at 10:01:31 UTC, SealabJaster wrote:
> 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/
Zig as well. And this creates many problems
- https://github.com/ziglang/zig/issues/10349
- https://github.com/ziglang/zig/issues/10196
- https://github.com/ziglang/zig/issues/3988
- https://github.com/ziglang/zig/issues/5426
- https://github.com/ziglang/zig/issues/8640
I esitated yesterday to reply to this thread because this can
appear as zig-bashing.
The reality is that I find that mixing expressions and statements
is not nice looking at all. From the last link:
```
const char = if (current < input.len)
input[current]
else
break;
```
- So the `break` expression has a type ? And this type is
compatible with the typê of `input[current]` ? wut ?
- expressions introduce scopes
- block terminators must be verified for each expressions
- etc.
More information about the Digitalmars-d
mailing list