C2 forum
General Category => Ideas => Topic started by: lerno on June 22, 2018, 05:33:25 PM
-
A typical while looks like this:
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:
while (i32 a = 10; a > 0) {
a--;
}
It would be equivalent to:
{
i32 a = 10;
while (a > 0) {
a--;
}
}
The same could be used for other similar statements:
// 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;
}
-
Making the scope of variables smaller is always a good thing.
C2 currently already supports the (C++) syntax of
while (Height h = getHeight()) {
..
}
So in terms of formal syntax, this means
while ( decl/expr ) {
instead of
while ( expr ) {
While making the scope smaller is nice, we also should try to allow avoid complex constructions like
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..
-
while (Height h = getHeight()) {
..
}
This code confused me totally. What this compiles to is apparently:
Height h = getHeight();
while (h) {
..
}
NOT what I would expect actually. I suggest that:
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
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:
// 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 ':').
-
Currently in C++ it's possible to do (using pointer now)
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.
-
Yes, but your current code does not match the C++ behaviour.
-
This is a bit similar to the Rust while-let construct, if we don't mind the patterns as C2 might never have those:
while let Some(i) = val {
// ...
}
In C2, I imagine that the example:
while (Height h = getHeight()) {
..
}
Should just compile to:
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
-
Yes, that is how it compiles in C++ also I guess.