Deutsch   English   Français   Italiano  
<pan$39bde$c80409dd$374fc6fd$c86207fe@invalid.invalid>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: Blue-Maned_Hawk <bluemanedhawk@invalid.invalid>
Newsgroups: comp.lang.c
Subject: Re: Named arguments in C
Date: Wed, 3 Jul 2024 20:42:22 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <pan$39bde$c80409dd$374fc6fd$c86207fe@invalid.invalid>
References: <utgjh0$21nsq$2@dont-email.me> <uth66l$266da$1@dont-email.me>
	<uti83u$2ed01$4@dont-email.me> <utjhfn$2r0cr$1@dont-email.me>
	<v61bbh$1n9ij$1@dont-email.me>
	<pan$1e5a0$ef4a1faf$e6cedebc$3f30bac1@invalid.invalid>
	<v61lm8$1p1gs$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 03 Jul 2024 22:42:22 +0200 (CEST)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="e7005f3dad67bc20d5ac43e33d5a6844";
	logging-data="2473663"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/knCKLRBRqF6P3JnPP2w8TmFrtVcWtQyU="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:y+yGbq6gBnoKP+etikTF7S/R73E=
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
 MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
 ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
 rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
 jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
 0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
 z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
 fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
 R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
 O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
 j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
 k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
 2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
 GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
 xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
X-Face: Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronuku
 pokaiwhenuakitanatahu
Bytes: 5108

bart wrote:

> On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
>> 
>> I searched around a bit, and it seems like a more common way to
>> implement named arguments in C is with a pattern like this:
>> 
>> #define f(...) f_impl((struct f_struct){__VA_ARGS__})
>> void f_impl(struct f_struct { int i, j;  char * k; double l, m, n; }
>> f_params)
>> {
>> 	/* actual code */
> 
> You missed out accesses to the parameters which would look like
> f_params.i and f_params.m.

Apologies—i assumed that it woudl be obvious that that's how it would be 
done.

>> }
>> 
>> int main(void)
>> {
>> 	f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
>> }
>> 
>> 
> This addresses a small part of it. Named parameters allow arguments to
> be omitted, and that requires also default values to be defined.

I think there's a difference of nomenclature here, because i would 
consider named parameters to _only_ imply the ability to name parameters, 
and not necessarily imply parameter omission.

Nevertheless, while searching around, i _did_ see people describe a way to 
assign default parameter values with a feature that i can confidently say 
i've never seen used anywhere else: the ability to specify designated 
initializers twice and have the latter override the first. 

> You can make ever more complex schemes to emulate them in C, but the
> boilerplate will just increase.

On the other hand, some boilerplate could be alleviated: we could get a 
result of

#define f(...) DEF(f, .i = 1, .j = 2, .k = "blah", __VA_ARGS__)

void DEC(f, int i, j; char * k; double l, m, n;)
{
	/* actual code */
}

through the macros

#define DEF(name, ...) name##_impl((struct name##_struct){__VA_ARGS__})

#define DEC(name, ...) name##_impl(struct name##_struct {__VA_ARGS__} 
name##_params)

which, while not perfect (i'm not a fan of the __VA_ARGS__ repetition 
necessary in DEF), do make things better and probably a little less error-
prone.

(Apparently, the P99 preprocessor library also has some macros in it to 
allow for default subroutine arguments.  I have done absolutely no 
research into how these work or whether they're any good.)

> But at least, this allows parameters with the same type to be declared
> as:
> 
>      double l, m, n
> 
> instead of:
> 
>      double l, double m, double n

I'm going to go out on a limb here and assume that the former form would 
have caused some sort of grammatical ambiguity/complexity issues when 
prestandard declarations were still in the standard, but _might_ be 
implementable nowadays now that they've been removed from the standard.



-- 
Blue-Maned_Hawk│shortens to Hawk│/blu.mɛin.dʰak/│he/him/his/himself/Mr.
blue-maned_hawk.srht.site
It would be disasterous!