Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams Newsgroups: comp.lang.c Subject: Re: on allowing "int a" definition everywhere Date: Thu, 22 Aug 2024 08:10:34 -0300 Organization: A noiseless patient Spider Lines: 56 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 22 Aug 2024 13:10:35 +0200 (CEST) Injection-Info: dont-email.me; posting-host="85c23c65ebcc3c54f3fb7aecb1a9f458"; logging-data="448107"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18eQ4QQDzmbSoQcc+sRa+RMC9BI4+us2bA=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:rdD7F6dAsE4NrYhTutbrzcRPVLw= Content-Language: en-US In-Reply-To: Bytes: 2394 On 22/08/2024 07:00, Bart wrote: > I assume the (...) tests the value of 'f'? If so then perhaps this is a > shorter way of expressing the same thing: > >     { >         FILE * f = fopen("file.txt", "r"); >         if (f) { >             /* … */ >             fclose(f); >         } >     } >     fwrite(f, …); Yes, this is the equivalent code. C++ allows two forms. One with explicit check and another one with implicit check. if (FILE* f = fopen("file.txt", "r"); f) /*f is checked explicitly*/ { /*...*/ fclose(f); } if (FILE* f = fopen("file.txt", "r")) /*f is checked implicitly*/ { /*...*/ fclose(f); } I also have considered for cake to have a extra part that is "defer" part. if (FILE* f = fopen("file.txt", "r"); f; fclose(f)) { } But if 'defer' were added to C2Y, we could then do the following: if (FILE* f = fopen("file.txt", "r"); f; ) { defer fclose(f); /*...*/ } Sometimes, too much syntactic sugar is not good because it creates too many variants.