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 03, 2022 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | 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. |
June 04, 2022 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Friday, 3 June 2022 at 18:33:57 UTC, Jack wrote: >Rust does support it like that:
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: But it is also available for D expression! Python example:
D example:
|
June 04, 2022 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SealabJaster | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SealabJaster | 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:
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:
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.
|
June 04, 2022 Re: Do you think if statement as expression would be nice to have in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | 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. |