C2 forum

General Category => Ideas => Topic started by: lerno on October 22, 2018, 09:31:55 PM

Title: In-loop definitions
Post by: lerno on October 22, 2018, 09:31:55 PM
This is a features I was considering for my own language.

Basically we recognize that in cases like this, we unnecessarily leak information to the outer scope:

Code: [Select]
int result;
if ((result = do_something()) != ERR_RESULT) {
  do_something_with_result(result);
}

Object *obj;
while ((obj = next_object()) != NULL) {
   ... do stuff ...
}

We could extend the syntax with an declaration:

Code: [Select]
if (int result; (result = do_something()) != ERR_RESULT) {
  do_something_with_result(result);
}

while (Object obj; (obj = next_object()) != NULL) {
   ... do stuff ...
}

switch (int result; (result = value())) {
  ...
}
 

There are a few more possibilities for separator:

Code: [Select]
while (Object obj : (obj = next_object()) != NULL) { 
   ...
}

while (Object obj, (obj = next_object()) != NULL) { 
   ...
}

while (Object obj | (obj = next_object()) != NULL) { 
   ...
}

while (Object obj :: (obj = next_object()) != NULL) { 
   ...
}

Obviously this is simply about extending what we have in "for" to a more general case.