Do you think if statement as expression would be nice to have in D?
user1234
user1234 at 12.de
Sat Jun 4 20:11:09 UTC 2022
On Saturday, 4 June 2022 at 16:55:37 UTC, user1234 wrote:
> On Saturday, 4 June 2022 at 16:28:07 UTC, Nick Treleaven wrote:
>> On Saturday, 4 June 2022 at 16:24:10 UTC, user1234 wrote:
>>> On Saturday, 4 June 2022 at 16:15:26 UTC, Nick Treleaven
>>> wrote:
>>>> On Saturday, 4 June 2022 at 11:35:41 UTC, user1234 wrote:
>>>>> ```
>>>>> 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 ?
>>>>
>>>> Just like `throw` is an expression in D now. It's type is
>>>> noreturn, which implicitly converts to any type.
>>>
>>> I think this does not give intuitively what char value will
>>> be: undefined, 0 ?
>>> Well in this case a loop is exited and char is out of scope
>>> so that does not matter but what if char is a var owned by
>>> the loop outer scope.
>>
>> In that case, no assignment would occur. Control flow was
>> interrupted before the assignment.
>
> yes, this is what I observe
>
> ```
> const std = @import("std");
>
> pub fn ert(input: []const u8) u8 {
> var ret: u8 = 0;
> var current: usize = 0;
> while (true) {
> ret = if (current < input.len - 1)
> input[current]
> else
> break;
> current += 1;
> }
> return ret;
> }
>
> pub fn main() void {
> const t : u8 = ert("ABC");
> std.debug.print("{}\n", .{t});
> }
> ```
>
>> 66
>
> I had no idea. Not mind changing however.
So actually there's not even something like a common type
involved. The "then" part is used as assign rhs and the "else"
part is just unrelated.
well "Okay" let's say ;) What a strange language tho.
More information about the Digitalmars-d
mailing list