June 03, 2022
On 6/3/22 11:33, Jack wrote:
> Rust does support it like that:
>
> ```rust
>      const limit:i32 = 30;
>      let n = 10;
>      let mut r = if n > limit { "big" } else { "small "};
>      println!("{} is {} than {}", n, r, limit);
> ```
>
> do you think would be nice that in D as well?

It is consistent with the rest of Rust because I think all blocks are expressions in Rust. For example:

  https://doc.rust-lang.org/reference/expressions/block-expr.html

Ali

June 04, 2022

On Friday, 3 June 2022 at 18:33:57 UTC, Jack wrote:

>

I find it can increase code reability...

I find it to be much noisier than e.g. auto r = (n > limit) ? "big" : "small";

June 04, 2022
On 6/3/2022 11:33 AM, Jack wrote:
> Rust does support it like that:
> 
> ```rust
>      const limit:i32 = 30;
>      let n = 10;
>      let mut r = if n > limit { "big" } else { "small "};
>      println!("{} is {} than {}", n, r, limit);
> ```
> 
> do you think would be nice that in D as well? I find it can increase code reability...

D has:

    auto r = (n > limit) ? "big" : "small";

which is more readable than Rust's :-)
June 04, 2022

On Friday, 3 June 2022 at 18:33:57 UTC, Jack wrote:

>

Rust does support it like that:

    const limit:i32 = 30;
    let n = 10;
    let mut r = if n > limit { "big" } else { "small "};
    println!("{} is {} than {}", n, r, limit);

do you think would be nice that in D as well? I find it can increase code reability...

Could you please provide more specific example, where if-expression could be useful?

There is a ConditionExpression in D, like it was mentioned previously. https://dlang.org/spec/expression.html#conditional_expressions

With fast googling I found only this argumentation:
The advantage of the inline if expression is that it's an expression, which means you can use it inside other expressions—list comprehensions, lambda functions, etc.

But it is also available for D expression!

Python example:

arr = ["Even" if i % 2 == 0 else "Odd" for i in range(10)]

D example:

auto arr2 = iota(10)
    .map!(x => x % 2 ? "Odd" : "Even");
June 04, 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/
June 04, 2022
I sometimes do occasionally run into a situation where I get the feeling it would be nice to embed a statement in the expression, but the feeling soon passes.

Having loops embedded in an expression is not very readable.
June 04, 2022
Here is a question:

How much work would it take to make it work for lets just say if, foreach and switch?

Because this reminds me of the old removal of an if statement for imports story.
June 04, 2022
On Saturday, 4 June 2022 at 10:13:15 UTC, rikki cattermole wrote:
> Here is a question:
>
> How much work would it take to make it work for lets just say if, foreach and switch?
>
> Because this reminds me of the old removal of an if statement for imports story.

Even if it's easy to add... I think "immediately invoked lambda" is good enough.


June 04, 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

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.
June 04, 2022

On Friday, 3 June 2022 at 18:33:57 UTC, Jack wrote:

>

do you think would be nice that in D as well? I find it can increase code reability...

No.

The feature comes from Ocaml, where match, if, and any statements are expressions.
Even there, it doesn't make things especially readable.
It's more about an ideology that all statements must be expressions.
Something that readable languages, like Python, Java, COBOL, BASIC... did not retain.