Author Topic: What about initialization in conditions?  (Read 152 times)

lerno

  • Full Member
  • ***
  • Posts: 159
    • View Profile
What about initialization in conditions?
« on: June 22, 2018, 05:33:25 PM »
A typical while looks like this:

Code: [Select]
i32 a = 10;
while (a > 0) {
    a--;
}

Often it's not really necessary to have the variable outside the while scope. We could support this syntax:

Code: [Select]
while (i32 a = 10; a > 0) {
    a--;
}

It would be equivalent to:

Code: [Select]
{
   i32 a = 10;
   while (a > 0) {
      a--;
   }
}

The same could be used for other similar statements:

Code: [Select]
// if
if (i32 a = foo(); a > 0) {
   // a is available here
} else {
   // a is also available here
}
// case
switch (Height h = getHeight(); h) {
   case LOW:     
   case MEDIUM:
      // can access "h" here
      break;
   case HIGH:
      // can access "h" here
      break;
}

bas

  • Full Member
  • ***
  • Posts: 177
    • View Profile
Re: What about initialization in conditions?
« Reply #1 on: June 23, 2018, 07:29:29 AM »
Making the scope of variables smaller is always a good thing.
C2 currently already supports the (C++) syntax of

Code: [Select]
while (Height h = getHeight()) {
  ..
}

So in terms of formal syntax, this means

Code: [Select]
while ( decl/expr ) {

instead of

Code: [Select]
while ( expr ) {

While making the scope smaller is nice, we also should try to allow avoid complex constructions like
Code: [Select]
while ( u32 x = get(), u32 y = getY(); x < y && x > 0) {

because this makes the code unreadable. Maybe a single decl would be a middle way that
covers most situations and avoids excess complexity..
« Last Edit: June 27, 2018, 08:59:00 AM by bas »

lerno

  • Full Member
  • ***
  • Posts: 159
    • View Profile
Re: What about initialization in conditions?
« Reply #2 on: June 23, 2018, 10:50:25 PM »
Code: [Select]
while (Height h = getHeight()) {
  ..
}

This code confused me totally. What this compiles to is apparently:

Code: [Select]
Height h = getHeight();
while (h) {
  ..
}

NOT what I would expect actually. I suggest that:

Code: [Select]
while (Height h = getHeight()) {
  ..
}

// => should not compile

while (Height h = getHeight(); h) {
  ..
}

// => compiles to

Height h = getHeight();
while (h) {
  ..
}

Finally we then can have have

Code: [Select]
while (Height h; h = getHeight()) {
  ..
}

// Compiles to
Height h;
while ((h = getHeight()) {
  ..
}

Since this is all new, it's possible to actually try different syntax from C:

Code: [Select]
// Separate by ':'
while (i32 y = 0 : y > 0) {
while (i32 y = 0; i32 z = 0 : y < 100 && z < 3) {

// Group multiple declarations in { }:
while ({i32 x = 0; i32 y = 100;}; x < 100 && y < 500) {

I'm a bit fond of the ":" version since it cleanly separates what only occurs before the while evaluation (left of ':'), and what is part of the repeated evaluation (right of ':').

bas

  • Full Member
  • ***
  • Posts: 177
    • View Profile
Re: What about initialization in conditions?
« Reply #3 on: June 27, 2018, 09:01:32 AM »
Currently in C++ it's possible to do (using pointer now)

Code: [Select]
while (Ptr* p = getPointer()) {
  ..
}

This compiles to the exact same thing. So each iteration, p is assigned a value from a call to getPointer().
So this call could have an internal iterator state and return NULL once it's done. These type of constructions
are also used in clang itself because they lower the scope of p.


lerno

  • Full Member
  • ***
  • Posts: 159
    • View Profile
Re: What about initialization in conditions?
« Reply #4 on: June 28, 2018, 07:19:37 PM »
Yes, but your current code does not match the C++ behaviour.

magnusi

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: What about initialization in conditions?
« Reply #5 on: November 09, 2018, 11:31:17 PM »
This is a bit similar to the Rust while-let construct, if we don't mind the patterns as C2 might never have those:

Code: [Select]
while let Some(i) = val {
        // ...
}

In C2, I imagine that the example:

Code: [Select]
while (Height h = getHeight()) {
  ..
}

Should just compile to:

Code: [Select]
Height h;
while (h = getHeight()) {
  ..
}

And I would say that's it. No separation, no other behavior, just something simple as this, so that too much new syntax isn't introduced and the complexity doesn't go up

bas

  • Full Member
  • ***
  • Posts: 177
    • View Profile
Re: What about initialization in conditions?
« Reply #6 on: November 13, 2018, 08:50:38 AM »
Yes, that is how it compiles in C++ also I guess.