Do you think if statement as expression would be nice to have in D?
Sergey
kornburn at yandex.ru
Sat Jun 4 10:00:35 UTC 2022
On Friday, 3 June 2022 at 18:33:57 UTC, 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...
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:
```python
arr = ["Even" if i % 2 == 0 else "Odd" for i in range(10)]
```
D example:
```d
auto arr2 = iota(10)
.map!(x => x % 2 ? "Odd" : "Even");
```
More information about the Digitalmars-d
mailing list