Liste des Groupes | Revenir à cl c |
On 04/04/2025 20:23, Thiago Adams wrote:Thanks for the comments.What do you think of this control block?As a new language feature?
>
do
{
FILE f = fopen("file.txt", "r");
>
if (f == NULL) quit; /*goes to else part*/
>
/*success here*/
for (int i =0; i < 10; i++){
...
if (error) quit;
}
>
}
else
{
/*some error*/
}
>
I think 'do {}' would be confused 'do {} while' (a parser - or a human - won't know this isn't a loop until the 'else' is seen. And then there's a possibility that that 'else' belongs to an 'if' but an omission or inclusion of a braces has screwed things up.
Perhaps a new keyword ('try' maybe) is better.try { quit; } else { } ?
You'd also need to specify how nested do-else blocks will work. Or will 'quit' (so you have a new keyword anyway) only work at this level?do {
>
What about chained versions: do {} else do {} else ...; I guess 'quit' will just go to the next else block rather than quit the whole chain?I'm already doing this with macros. The problem is defining the right keywords. In the past, I used keywords like try, catch, and throw, but people didn’t like the feature. throw is indeed a little confusing since it’s not actually throwing anything.
Note that this can be emulated in C now using:
if
{
goto quit001;
}
else
{quit001:
}
Perhaps what C needs is a way to do that jump without an explicit goto:
if
{
quit001;
}
else
{quit001:
}
You can require that such jumps can only go forwards.
Les messages affichés proviennent d'usenet.