June 04, 2022

On Saturday, 4 June 2022 at 11:35:41 UTC, user1234 wrote:

>
const char = if (current < input.len)
            input[current]
        else
            break;

It's too ugly!

June 04, 2022

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.

June 04, 2022

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.

June 04, 2022

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.

June 04, 2022

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.

June 04, 2022
On 6/4/2022 3:13 AM, rikki cattermole wrote:
> How much work would it take to make it work for lets just say if, foreach and switch?

Using lambdas would be better.
June 04, 2022

On Saturday, 4 June 2022 at 10:01:31 UTC, SealabJaster wrote:

>

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;
}
auto number = 200;
auto myVar = type == "as-is"?
  number:
  { number *= 2;
    number += 2;
    number /= 2;
    number = number > 300 ? 200 : 100;
    return number;
  }();
June 04, 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.

June 04, 2022

On Saturday, 4 June 2022 at 20:11:09 UTC, user1234 wrote:

>

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.

imagine using a switch as assign lhs too:

```
    int a,b,c;
    switch (cond)
    {
        case 0: a; break;
        case 1: b; break;
        default: c;
    } = 0;
```

that whole thing of mixing expressions and statements looks really absurd even if there are special cases that may appear appealing.

In this last example you'd have to check that every case yields a compatible lvalue.
So this bring the fact that statements also have a value category !

Now maybe you also want chained assignments.

```
    int nope, a,b,c;
    nope = switch (cond)
    {
        case 0: a; break;
        case 1: b; break;
        default: c;
    } = 0;
```

No way.

June 04, 2022

On Saturday, 4 June 2022 at 19:54:48 UTC, Dukc wrote:

>
auto number = 200;
auto myVar = type == "as-is"?
  number:
  { number *= 2;
    number += 2;
    number /= 2;
    number = number > 300 ? 200 : 100;
    return number;
  }();

This next question comes from a place of ignorance: What is the codegen like for this code? Would is allocate a closure on the GC before performing the execution, or are the compilers smart enough to inline the entire thing?