Liste des Groupes | Revenir à cl c |
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: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.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.
>
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).
Les messages affichés proviennent d'usenet.