Re: Named arguments in C

Liste des GroupesRevenir à cl c  
Sujet : Re: Named arguments in C
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 04. Jul 2024, 01:12:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v64pdt$2dlb6$1@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 03/07/2024 21:42, Blue-Maned_Hawk wrote:
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.
Part of the reason for using the feature is for simpler, clearer code. But such solutions have costs in the form of extra syntax, extra clutter that can obscure what you're trying to do.
So I think it was important to show that reflected in your example.
A built-in named-argument feature would not require any changes to the body of a function. The only difference might be in specifying default values for optional arguments.

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.)
This is all very ingenious, but I doubt the results are worth it in practice. What short of error messages are you likely to get for a typo for example?
My language, which has the feature, would define the same function like this:
     proc f(int i=1, j=2, ichar k="blah", real l, m, n) =
         ....
     end
It might be called like this:
     f(l:10, m:20, n:30)
(The floats are the only ones that can't be omitted.)
It really needs language support. That has been done in C for designated initialisers; named args are a similar feature, easier to implement (they can only be one level deep for example) and IMO far more useful.
Although there are a few extra problems with C because the extra info needed (parameter names and default values) can appear in both the definition, and any number of prototype declarations, which cannot in conflict.

Date Sujet#  Auteur
21 Mar 24 * Block Comments Or Rest-Of-Line Comments?72Lawrence D'Oliveiro
21 Mar 24 +- Re: Block Comments Or Rest-Of-Line Comments?1bart
21 Mar 24 +* Re: Block Comments Or Rest-Of-Line Comments?31Mikko
21 Mar 24 i`* Re: Block Comments Or Rest-Of-Line Comments?30Lawrence D'Oliveiro
22 Mar 24 i `* Re: Block Comments Or Rest-Of-Line Comments?29David Brown
22 Mar 24 i  +- Re: Block Comments Or Rest-Of-Line Comments?1Blue-Maned_Hawk
23 Mar 24 i  +* Re: Block Comments Or Rest-Of-Line Comments?5Lawrence D'Oliveiro
23 Mar 24 i  i+* Re: Block Comments Or Rest-Of-Line Comments?2Chris M. Thomasson
23 Mar 24 i  ii`- Re: Block Comments Or Rest-Of-Line Comments?1Lawrence D'Oliveiro
23 Mar 24 i  i`* Re: Block Comments Or Rest-Of-Line Comments?2David Brown
23 Mar 24 i  i `- Re: Block Comments Or Rest-Of-Line Comments?1Malcolm McLean
2 Jul 24 i  `* Named arguments in C22Ivan Farlenkov
2 Jul 24 i   +* Re: Named arguments in C12Blue-Maned_Hawk
2 Jul 24 i   i+* Re: Named arguments in C4Richard Harnden
3 Jul 24 i   ii`* Re: Named arguments in C3Blue-Maned_Hawk
4 Jul 24 i   ii `* Re: Named arguments in C2Richard Harnden
4 Jul 24 i   ii  `- Re: Named arguments in C1Kaz Kylheku
2 Jul 24 i   i`* Re: Named arguments in C7bart
3 Jul 24 i   i +- Re: Named arguments in C1Keith Thompson
3 Jul 24 i   i +* Re: Named arguments in C4Blue-Maned_Hawk
4 Jul 24 i   i i`* Re: Named arguments in C3bart
4 Jul 24 i   i i `* Re: Named arguments in C2Keith Thompson
4 Jul 24 i   i i  `- Re: Named arguments in C1Kaz Kylheku
4 Jul 24 i   i `- Re: Named arguments in C1Opus
3 Jul 24 i   +- Re: Named arguments in C1Lawrence D'Oliveiro
4 Jul 24 i   `* Re: Named arguments in C8Bonita Montero
4 Jul 24 i    +* Re: Named arguments in C2Michael S
4 Jul 24 i    i`- Re: Named arguments in C1Michael S
4 Jul 24 i    `* Re: Named arguments in C5bart
4 Jul 24 i     `* Re: Named arguments in C4Bonita Montero
4 Jul 24 i      +- Re: Named arguments in C1Bonita Montero
4 Jul 24 i      `* Re: Named arguments in C2bart
4 Jul 24 i       `- Re: Named arguments in C1Bonita Montero
21 Mar 24 +- Re: Block Comments Or Rest-Of-Line Comments?1Blue-Maned_Hawk
21 Mar 24 +* Re: Block Comments Or Rest-Of-Line Comments?4Tim Rentsch
21 Mar 24 i`* Re: Block Comments Or Rest-Of-Line Comments?3Dan Cross
22 Mar 24 i `* Re: Block Comments Or Rest-Of-Line Comments?2Tim Rentsch
23 Mar 24 i  `- Re: Block Comments Or Rest-Of-Line Comments?1Dan Cross
21 Mar 24 +* Re: Block Comments Or Rest-Of-Line Comments?16David Brown
21 Mar 24 i`* Re: Block Comments Or Rest-Of-Line Comments?15Richard Harnden
21 Mar 24 i +* Re: Block Comments Or Rest-Of-Line Comments?13Keith Thompson
21 Mar 24 i i+* Re: Block Comments Or Rest-Of-Line Comments?5Richard Harnden
21 Mar 24 i ii`* Re: Block Comments Or Rest-Of-Line Comments?4Keith Thompson
21 Mar 24 i ii +- Re: Block Comments Or Rest-Of-Line Comments?1Richard Harnden
21 Mar 24 i ii `* Re: Block Comments Or Rest-Of-Line Comments?2Lawrence D'Oliveiro
22 Mar 24 i ii  `- Re: Block Comments Or Rest-Of-Line Comments?1Keith Thompson
21 Mar 24 i i+* Re: Block Comments Or Rest-Of-Line Comments?2Kaz Kylheku
22 Mar 24 i ii`- Re: Block Comments Or Rest-Of-Line Comments?1Keith Thompson
21 Mar 24 i i+* Re: Block Comments Or Rest-Of-Line Comments?2Kaz Kylheku
27 Apr 24 i ii`- Re: Block Comments Or Rest-Of-Line Comments?1paul
22 Mar 24 i i`* Re: Block Comments Or Rest-Of-Line Comments?3James Kuyper
22 Mar 24 i i +- Re: Block Comments Or Rest-Of-Line Comments?1Keith Thompson
23 Mar 24 i i `- Re: Block Comments Or Rest-Of-Line Comments?1Lawrence D'Oliveiro
22 Mar 24 i `- Re: Block Comments Or Rest-Of-Line Comments?1David Brown
21 Mar 24 +* Re: Block Comments Or Rest-Of-Line Comments?2fir
21 Mar 24 i`- Re: Block Comments Or Rest-Of-Line Comments?1Lawrence D'Oliveiro
23 Mar 24 +- Re: Block Comments Or Rest-Of-Line Comments?1Malcolm McLean
23 Apr 24 +* Re: Block Comments Or Rest-Of-Line Comments?9DFS
24 Apr 24 i+- Re: Block Comments Or Rest-Of-Line Comments?1Lawrence D'Oliveiro
24 Apr 24 i`* Re: Block Comments Or Rest-Of-Line Comments?7Blue-Maned_Hawk
24 Apr 24 i +* Re: Block Comments Or Rest-Of-Line Comments?2Sjouke Burry
25 Apr 24 i i`- Re: Block Comments Or Rest-Of-Line Comments?1Blue-Maned_Hawk
25 Apr 24 i `* Re: Block Comments Or Rest-Of-Line Comments?4DFS
26 Apr 24 i  `* Re: Block Comments Or Rest-Of-Line Comments?3Blue-Maned_Hawk
26 Apr 24 i   +- Re: Block Comments Or Rest-Of-Line Comments?1Chris M. Thomasson
26 Apr 24 i   `- Re: Block Comments Or Rest-Of-Line Comments?1DFS
23 Apr 24 +* Re: Block Comments Or Rest-Of-Line Comments?5Kaz Kylheku
24 Apr 24 i`* Re: Block Comments Or Rest-Of-Line Comments?4Lowell Gilbert
24 Apr 24 i +* Re: Block Comments Or Rest-Of-Line Comments?2Ben Bacarisse
24 Apr 24 i i`- Re: Block Comments Or Rest-Of-Line Comments?1Lowell Gilbert
24 Apr 24 i `- Re: Block Comments Or Rest-Of-Line Comments?1Kaz Kylheku
27 Apr 24 `- Re: Block Comments Or Rest-Of-Line Comments?1paul

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal