Liste des Groupes | Revenir à cl c |
On 02/03/2025 11:52, David Brown wrote:Sure.On 02/03/2025 02:24, Keith Thompson wrote:I've seen examples of emulating named parameters in C using structs. They had multiple issues:Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:>On 01.03.2025 23:20, Lawrence D'Oliveiro wrote:>On Sat, 1 Mar 2025 21:32:59 +0100, Janis Papanagnou wrote:>My guess is that this form is just an informal syntax mimicking Perl's>
function parameter passing ...
The idea of passing arguments by keyword predates Perl.
That wouldn't surprise me. - I know it from Perl. - Which other
(earlier) languages do you have in mind? - What's its origin?
For example, it exists in Ada since 1983. The following are all
equivalent, given that Foo takes integer arguments Arg1 and Arg2:
>
Foo(Arg1 => 10, Arg2 => 20);
Foo(Arg2 => 20, Arg1 => 10);
Foo(10, Arg2 => 20);
Foo(10, 20);
>
The syntax is IMHO much nicer than using /*...*/ comments in C.
>
Ada (and Python - no idea about Perl) named parameter passing have two huge advantages over comments like Lawrence uses - one is that they let you order the parameters in what you see as a logical manner at the call source (especially useful combined with default parameters), and the other is that the whole thing is checked by the compiler. If the function's signature is changed, parameters re-ordered or renamed, the call source is either still correct or there is a hard compile-time error. With comment-based naming, all you have done is guaranteed that the call source looks correct but is wrong. Using comments like Lawrence does is significant extra effort (especially to keep it in sync with changes), and can't be trusted. It's all cost and no gain.
>
Named parameters are a useful tool when the language provides them, but useless when done with comments.
>
If someone wants to have the benefits of named parameter passing in C, then using a struct for the arguments along with designated initialisers and compound literals is a much better way. Individual types for the different parameters also works (wrapping the desired types in structs).
* Having to invent auxiliary struct types for each kind of function signature
* Having to rewrite function bodies to access parameters as struct elements instead
* (Within the implementation, probably having to set up then copy the struct to preserve value-passing semantics)
* At the call-site, needing to use an ugly, cluttered syntax involving compound literals and designated initialisers, a lot worse than the example with /**/ comments
* Being limited in the default values for omitted arguments, which can only be zeros
* Not being able to superimpose named parameters on imported functions of existings libraries, since they require the function source code to be revised and recompiled. (In my implementations of the feature, this is possible, while none of the above drawbacks apply.)
It's better to accept that the language doesn't have the feature.Agreed.
However, it is similar to designated initialisers, and would have been a much more useful feature for C99.I'd like to see named function parameters as part of C. But I'm not holding my breath waiting for them!
Les messages affichés proviennent d'usenet.