Deutsch   English   Français   Italiano  
<vsotn2$3nn12$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: "A diagram of C23 basic types"
Date: Fri, 4 Apr 2025 17:26:26 +0200
Organization: A noiseless patient Spider
Lines: 145
Message-ID: <vsotn2$3nn12$2@dont-email.me>
References: <87y0wjaysg.fsf@gmail.com> <vsj1m8$1f8h2$1@dont-email.me>
 <vsj2l9$1j0as$1@dont-email.me> <vsjef3$1u4nk$1@dont-email.me>
 <vsjg6t$20pdb$1@dont-email.me> <vsjjd1$23ukt$1@dont-email.me>
 <vskb2m$2scqh$1@dont-email.me> <vslis6$8mfb$2@dont-email.me>
 <vsnhdu$297ra$1@dont-email.me> <vsondc$3htha$2@dont-email.me>
 <vsopgk$3g9f5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 04 Apr 2025 17:26:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c769e35c057c19c3049ecfc31d4accc3";
	logging-data="3922978"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19GAGYoT0aIJmxdJ2XLDefhBpJFjHyVVwE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.11.0
Cancel-Lock: sha1:GEx+GxqTIKVsQHPTh3PTZIUxUfo=
Content-Language: en-GB
In-Reply-To: <vsopgk$3g9f5$1@dont-email.me>
Bytes: 5777

On 04/04/2025 16:14, bart wrote:
> On 04/04/2025 14:38, David Brown wrote:
>> On 04/04/2025 04:50, Janis Papanagnou wrote:
> 
>>> Really, that recent!? - I was positive that I used it long before 2017
>>> during the days when I did quite regularly C++ programming. - Could it
>>> be that some GNU compiler (C++ or "C") supported that before it became
>>> C++ standard?
>>>
>>> Janis
>>>
>>
>> To be clear, we are talking about :
>>
>>      if (int x = get_next_value(); x > 10) {
>>          // We got a big value!
>>      }
>>
>> It was added in C++17.  <https://en.cppreference.com/w/cpp/language/if>
>>
>> gcc did not have it as an extension, but they might have had it in the 
>> pre-standardised support for C++17 (before C++17 was published, gcc 
>> had "-std=c++1z" to get as many proposed C++17 features as possible 
>> before they were standardised.  gcc has similar "pre-standard" support 
>> for all C and C++ versions).
> 
> So, this is a proposal still for C, as it doesn't work for any current 
> version of C (I should have read the above more carefully first!).

Yes, that is correct.  The feature has made it into the public drafts 
for the post-C23 version of C standards, but I have no idea when that 
will be complete.

> 
> There are appear to be two new features:
> 
> * Allowing a declaration where a conditional expresson normally goes

Yes.

> 
> * Having several things there separated with ";" (yes, here ";" is a 
> separator, not a terminator).

Two things, rather than several.

Thus:

	if (int x = get_next_value()) {
		...
	}

is equivalent to :

	if (int x = get_next_value(); x) {
		...
	}

and

	{
		int x = get_next_value();
		if (x) {
			...
		}
	}

<https://open-std.org/JTC1/SC22/WG14/www/docs/n3467.pdf>
Page 163 (labelled page numbers) or page 179 (of the pdf).

> 
> Someone said they weren't excited by my proposal of being able to leave 
> out '#include <stdio.>'. Well I'm not that excited by this.

OK.  I would not expect you to be - you prefer your variables to be 
declared at the head of functions rather than at minimal scope.  For 
other C programmers, this is similar to "for (int i = 0; i < 10; i++)" 
and will likely be quite popular once C23 support gets established.

> 
> In fact I would actively avoid such a feature, as it adds clutter to 
> code. It might look at first as though it saves you having to add a 
> separate declaration, until you're writing the pattern for the fourth 
> time in your function and realised you now have 4 declarations for 'x'!
> 
> And also the type of 'x' is hardcoded in four places instead of one (so 
> if 'get_next_value' changes its return type, you now have more 
> maintenance and a real risk of missing out one).
> 

I think your counting is off.

	if (int x = get_next_value()) { ... }

is /one/ use of "x".

A C90 style of :

	int x;
	...
	x = get_next_value();
	if (x) { ... }

is /three/ uses of "x".


Different arrangements and conditionals will have different counts, of 
course.

But a major point of having small scopes is that the length and 
description power of an identifier should be roughly proportional to its 
scope.  A variable that exists throughout a sizeable function needs a 
longer and more descriptive name than a short-lived variable in a small 
scope, whose purpose is immediately obvious from a few lines of code.


> (If you say that those 4 instances could call different functions so 
> each 'x' is a different type, then it would be a different kind of 
> anti-pattern.)
> 
> Currently it would need this (it is assumed that 'x' is needed in the 
> body):
> 
>      int x;
> 
>      if ((x = getnextvalue()) > 10) {
>            // We got a big value!
>      }
> 
> It's a little cleaner. (Getting rid of those outer parameter parentheses 
> would be far more useful IMO.)
> 
> (My language can already do this stuff:
> 
>      if int x := get_next_value(); x > 10 then
>          println "big"
>      fi
> 
> But it is uncommon, and it would be over-cluttery even here. However I 
> don't have the local scope of 'x' that presumably is the big deal in the 
> C++ feature.)

Yes, small scope is the point.  Small scopes are better than big scopes 
(within reason).