The original comment delimiters in C were copied from PL/I: everything between “/*” and “*/” is a comment, even extending across multiple lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of the line was a comment. And C++ adopted a similar rule, using “//” as their to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the rest-of-line form in preference to the block form, and I’m not sure why. I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Do you feel the same?
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
The original comment delimiters in C were copied from PL/I: everything between “/*” and “*/” is a comment, even extending across multiple lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of the line was a comment. And C++ adopted a similar rule, using “//” as their to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the rest-of-line form in preference to the block form, and I’m not sure why. I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Do you feel the same?
The original comment delimiters in C were copied from PL/I: everything
The original comment delimiters in C were copied from PL/I: everything between “/*” and “*/” is a comment, even extending across multiple lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of the line was a comment. And C++ adopted a similar rule, using “//” as their to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the rest-of-line form in preference to the block form, and I’m not sure why. I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Do you feel the same?
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
The original comment delimiters in C were copied from PL/I: everything
Cite?
On 21/03/2024 07:19, Lawrence D'Oliveiro wrote:
The original comment delimiters in C were copied from PL/I: everything
between “/*” and “*/” is a comment, even extending across multiple lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of the
line was a comment. And C++ adopted a similar rule, using “//” as their >> to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the
rest-of-line
form in preference to the block form, and I’m not sure why. I’m fond of >> writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
I use both - block comments when making a comment block, and line
comments when adding comments to the end of a line. That seems pretty obvious to me.
scott@slp53.sl.home (Scott Lurndal) writes:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
The original comment delimiters in C were copied from PL/I: everything
Cite?
Seen in the B language manual:
Comments are delimited as in PL/I by /* and */.
https://www.bell-labs.com/usr/dmr/www/kbman.html
Note that this style of commenting was not present in
B's precursor language, BCPL.
The original comment delimiters in C were copied from PL/I: everything between “/*” and “*/” is a comment, even extending across multiple lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of the line was a comment. And C++ adopted a similar rule, using “//” as their to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the rest-of-line form in preference to the block form, and I’m not sure why. I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Do you feel the same?
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
I think you mean "#if 0".
I use that sometimes, but one disadvantage is that if you're viewing the middle of a very large block of code, it can be hard to tell that it's
been "commented" out.
I have a script that applies "#if 0" and "#endif" to a block of code
*and* prepends "* " to each line in the block.
Richard Harnden <richard.nospam@gmail.invalid> writes:
On 21/03/2024 19:23, Keith Thompson wrote:
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of >>>> code I don't want right now:I think you mean "#if 0".
#ifdef 0
...
#endif
Yes I did :)
I use that sometimes, but one disadvantage is that if you're viewing
the
middle of a very large block of code, it can be hard to tell that it's
been "commented" out.
I have a script that applies "#if 0" and "#endif" to a block of code
*and* prepends "* " to each line in the block.
That's a good ideo. Can you share it?
Sure. It's a Perl script called "if0". It tries to deal with
variations in line endings (I sometimes work with code with LF and/or
CRLF line endings) and with tabs vs. spaces as indentation.
I don't have a script that undoes what if0 does. I might write one
one of these days, but usually I can just revert the change in source
control or change it back manually.
Complaints about Perl being write-only will be cheerfully ignored.
```
#!/usr/bin/perl
use strict;
use warnings;
my @lines = <>;
my $newline = "\n";
if (defined $lines[0] and $lines[0] =~ /\r$/) {
$newline = "\r\n";
}
print "#if 0$newline";
foreach my $line (@lines) {
if ($line =~ /^ /) {
$line =~ s/ /*/;
}
elsif ($line =~ /^\r?$/) {
$line =~ s/^/*/;
}
else {
$line =~ s/^/* /;
}
print $line;
}
print "#endif /* 0 */$newline";
```
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
I think you mean "#if 0".
I use that sometimes, but one disadvantage is that if you're viewing the >middle of a very large block of code, it can be hard to tell that it's
been "commented" out.
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
I think you mean "#if 0".
I use that sometimes, but one disadvantage is that if you're viewing the middle of a very large block of code, it can be hard to tell that it's
been "commented" out.
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
I think you mean "#if 0".
I use that sometimes, but one disadvantage is that if you're viewing the >>middle of a very large block of code, it can be hard to tell that it's
been "commented" out.
Often syntax highlighting (e.g. vim) will solve that.
In projects with many fingers in the pie, I generally prefer either a descriptive (and undefined) macro name or a block comment in
the #if 0 region to indicate exactly _why_ it was not just removed
from the source file entirely.
Makes it easier for whomever is reading and/or maintaining the
code to follow it.
... mostly i use // coments becouse i nearly
exclusively use comments to comment out code and in editor i got it
under control+shift+c
On 2024-03-21 06:19:13 +0000, Lawrence D'Oliveiro said:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame, /*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx, /*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
I prefer to put the argument names at the end of the line.
It's a Perl script ...
On 21/03/2024 15:20, David Brown wrote:
On 21/03/2024 07:19, Lawrence D'Oliveiro wrote:
The original comment delimiters in C were copied from PL/I: everything
between “/*” and “*/” is a comment, even extending across multiple >>> lines.
Pascal had something similar, only the delimiters were “{” and “}”, or
“(*” and “*)” for compatibility with machines with restricted character
sets.
For some reason, the Ada folks decided block comments were not a good
idea, and so their rule was that anything after “--” up to the end of >>> the
line was a comment. And C++ adopted a similar rule, using “//” as their >>> to-end-of-line comment marker, though of course they also kept C-style
block comments. Java also keeps both these styles.
Since then, I’ve seen newer programmers gravitate towards the
rest-of-line
form in preference to the block form, and I’m not sure why. I’m fond of >>> writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this >>> part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
I use both - block comments when making a comment block, and line
comments when adding comments to the end of a line. That seems pretty
obvious to me.
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
On Thu, 21 Mar 2024 13:37:57 +0200, Mikko wrote:
On 2024-03-21 06:19:13 +0000, Lawrence D'Oliveiro said:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame, /*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx, /*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
I prefer to put the argument names at the end of the line.
But putting them in front of the values looks more like the syntax in languages (like Ada and Python) that do allow specification of argument keywords.
And maybe, in future, if it becomes valid in C (or some successor), then updating the code should be as simple as removing the comment symbols.
In article <86r0g3liii.fsf@linuxsc.com>,
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
scott@slp53.sl.home (Scott Lurndal) writes:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
The original comment delimiters in C were copied from PL/I: everything >>>Cite?
Seen in the B language manual:
Comments are delimited as in PL/I by /* and */.
https://www.bell-labs.com/usr/dmr/www/kbman.html
Note that this style of commenting was not present in
B's precursor language, BCPL.
It would be appropriate to qualify this statement with some kind
of timeframe as BCPL is still used (albeit not widely, I assume).
Why would you want your C code to look like Python? That's as silly as wanting your Python code to look like C code. The languages are significantly different - each with their strengths and weaknesses, and
each suitable for quite different kinds of programming tasks.
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
And sometimes, when it's not a really a comment, but rather a block of
code I don't want right now:
#ifdef 0
...
#endif
I think you mean "#if 0".
cross@spitfire.i.gajendra.net (Dan Cross) writes:
In article <86r0g3liii.fsf@linuxsc.com>,
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
scott@slp53.sl.home (Scott Lurndal) writes:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
The original comment delimiters in C were copied from PL/I: everything >>>>Cite?
Seen in the B language manual:
Comments are delimited as in PL/I by /* and */.
https://www.bell-labs.com/usr/dmr/www/kbman.html
Note that this style of commenting was not present in
B's precursor language, BCPL.
It would be appropriate to qualify this statement with some kind
of timeframe as BCPL is still used (albeit not widely, I assume).
The PL/I style of commenting was not present in BCPL at the time B
was being defined.
... I have occasionally
used #ifdef with a macro name that I haven't #defined, and have no plans
to #define, but which describes the reason why I'm commenting this
section out.
On 21/03/2024 22:16, Lawrence D'Oliveiro wrote:
On Thu, 21 Mar 2024 13:37:57 +0200, Mikko wrote:
On 2024-03-21 06:19:13 +0000, Lawrence D'Oliveiro said:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame, /*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx, /*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
I prefer to put the argument names at the end of the line.
But putting them in front of the values looks more like the syntax in
languages (like Ada and Python) that do allow specification of argument
keywords.
And maybe, in future, if it becomes valid in C (or some successor),Why would you want your C code to look like Python?
then updating the code should be as simple as removing the comment
symbols.
On 3/22/2024 7:58 PM, Lawrence D'Oliveiro wrote:
On Fri, 22 Mar 2024 10:02:47 +0100, David Brown wrote:
On 21/03/2024 22:16, Lawrence D'Oliveiro wrote:
On Thu, 21 Mar 2024 13:37:57 +0200, Mikko wrote:Why would you want your C code to look like Python?
On 2024-03-21 06:19:13 +0000, Lawrence D'Oliveiro said:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame, /*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx, /*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Is “Python” some kind of trigger word with you? Soon as you see that, a >> switch clicks off in your brain, and you are incapable of focussing on
anything else that was said?
Notice the name of the group?
On Fri, 22 Mar 2024 10:02:47 +0100, David Brown wrote:
On 21/03/2024 22:16, Lawrence D'Oliveiro wrote:
On Thu, 21 Mar 2024 13:37:57 +0200, Mikko wrote:Why would you want your C code to look like Python?
On 2024-03-21 06:19:13 +0000, Lawrence D'Oliveiro said:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame, /*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx, /*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
I prefer to put the argument names at the end of the line.
But putting them in front of the values looks more like the syntax in
languages (like Ada and Python) that do allow specification of argument
keywords.
And maybe, in future, if it becomes valid in C (or some successor),
then updating the code should be as simple as removing the comment
symbols.
Is “Python” some kind of trigger word with you? Soon as you see that, a switch clicks off in your brain, and you are incapable of focussing on anything else that was said?
I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
Do you feel the same?
Kaz Kylheku <643-408-1753@kylheku.com> writes:
A normal person won't compulsively comment very function argument,
Well, they might if they're generating documentation from the
comments.
A normal person won't compulsively comment very function argument,
Kaz Kylheku <643-408-1753@kylheku.com> writes:
A normal person won't compulsively comment very function argument,
Well, they might if they're generating documentation from the comments.
Even then, it would usually only be a concern for API-type functions,
which means a fairly small share of a typical codebase.
Lowell Gilbert <lgusenet@be-well.ilk.org> writes:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
A normal person won't compulsively comment very function argument,
Well, they might if they're generating documentation from the
comments.
The discussion was about function arguments in a call. Documentation
would, presumably, be generated from comments on (formal) parameters in
a function declaration.
On 3/21/2024 2:19 AM, Lawrence D'Oliveiro wrote:
I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this part
of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Get yourself some Notepad++.
Highlight a block of comments and hit Ctrl + Q.
Get yourself some Notepad++.
DFS wrote:
Get yourself some Notepad++.
Not everybody can afford a machine powerful enough for a fancy text editor like that.
On 24.04.24 14:36, Blue-Maned_Hawk wrote:
DFS wrote:BULSHIT.
Get yourself some Notepad++.
Not everybody can afford a machine powerful enough for a fancy text
editor like that.
I run notepad++ on a 20 year old xp pro machine, and never had a
problem.
DFS wrote:
Get yourself some Notepad++.
Not everybody can afford a machine powerful enough for a fancy text editor like that.
On 4/24/2024 8:36 AM, Blue-Maned_Troll wrote:
DFS wrote:
Get yourself some Notepad++.
Not everybody can afford a machine powerful enough for a fancy text
editor like that.
Notepad++ will run on $25 dumpster hardware.
DFS wrote:
On 4/24/2024 8:36 AM, Blue-Maned_Troll wrote:
That's an unsubstantiated claim.
DFS wrote:
Get yourself some Notepad++.
Not everybody can afford a machine powerful enough for a fancy text
editor like that.
Notepad++ will run on $25 dumpster hardware.
So what? Not everybody can afford a machine that powerful.
I have a simpler approach: commits which introduced commented-out
code, whether with #if 0, or any other means, shall not be merged.
I don't perpetrate that in my open source projects, and "-1" such
submissions at work.
When someone wants to remove code, I encourage them to actually
delete it. The comment about why it was deleted goes into the
commit log.
Since then, I’ve seen newer programmers gravitate towards the rest-of-line form in preference to the block form, and I’m not sure why. I’m fond of writing things like
/*
A very simple HTML/XML entity-escape function--why isn’t this
part of the standard Java API?
*/
which involve less typing than
//
// A very simple HTML/XML entity-escape function--why isn’t this
// part of the standard Java API?
//
Also, the “block” form allows “interspersed” comments, where a short comment can be put in the middle of a line and followed by more program
text in the rest of the line. For example, as a way of keeping long
argument lists straight:
gdImageCopyResampled
(
/*dst =*/ ResizedFrame,
/*src =*/ Context.StillFrame,
/*dstX =*/ 0,
/*dstY =*/ 0,
/*srcX =*/ 0,
/*srcY =*/ 0,
/*dstW =*/ ResizedFrame->sx,
/*dstH =*/ ResizedFrame->sy,
/*srcW =*/ Context.StillFrame->sx,
/*srcH =*/ Context.StillFrame->sy
);
I am a fan of being able to name parameters in languages that allow it. I am quite confident that this will never come to C. It /might/ make it into C++, but as people have been writing proposals to do so for 20 years at least, I am not holding mybreath.
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 */
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
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 */
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
You can sort of already do it in C by using designated initializers and macros
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
That's the kind of thing Bonita would write.
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 */
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
Horrible.
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.
}This addresses a small part of it. Named parameters allow arguments to
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
be omitted, and that requires also default values to be defined.
You can make ever more complex schemes to emulate them in C, but the boilerplate will just increase.
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
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.
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.)
bart <bc@freeuk.com> writes:
[...]
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.
As I recall, we had this discussion here a while ago. The fact that C
allows parameter names for a function definition to differ from those in corresponding declarations is a bit inconvenient. But what I recall suggesting at the time is that the parameter names in a call need to be consistent with the names in the visible declaration.
void foo(int x, int y) {
// ...
}
void foo(int xx, int yy);
foo(xx: 10, yy: 20);
Richard Harnden wrote:
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
That's the kind of thing Bonita would write.
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 */
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
Horrible.
I assure you that i can write much worse code. What do you say makes this
is horrible?
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
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.
On 03/07/2024 21:16, Blue-Maned_Hawk wrote:
Richard Harnden wrote:
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
That's the kind of thing Bonita would write.
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 */
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
Horrible.
I assure you that i can write much worse code. What do you say makes this >> is horrible?
It just doesn't feel like how C ought to be, not to me anyway.
If you have named parameters, then you have to allow default values.
Then you have function overloading.
Am 02.07.2024 um 18:53 schrieb Ivan Farlenkov:
I am a fan of being able to name parameters in languages thatYou can sort of already do it in C by using designated initializers
allow it. I am quite confident that this will never come to C. It
/might/ make it into C++, but as people have been writing
proposals to do so for 20 years at least, I am not holding my
breath.
and macros
#define foo(A, B, C, ...) do{\
stuct technical technical={\
var1=default1,\
var2=default2,\
var3=default3 __VA_OPT__(,)\
__VA_ARGS__\
}\
actual_foo(A, B, C, technical.var1, technical.var2,
technical.var3)\ }while(0)
What an anquated language that this needs macros.
Am 02.07.2024 um 18:53 schrieb Ivan Farlenkov:
I am a fan of being able to name parameters in languages that allowYou can sort of already do it in C by using designated initializers
it. I am quite confident that this will never come to C. It /might/
make it into C++, but as people have been writing proposals to do so
for 20 years at least, I am not holding my breath.
and macros
#define foo(A, B, C, ...) do{\
stuct technical technical={\
var1=default1,\
var2=default2,\
var3=default3 __VA_OPT__(,)\
__VA_ARGS__\
}\
actual_foo(A, B, C, technical.var1, technical.var2, technical.var3)\
}while(0)
What an anquated language that this needs macros.
On Thu, 4 Jul 2024 13:12:18 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
Am 02.07.2024 um 18:53 schrieb Ivan Farlenkov:
I am a fan of being able to name parameters in languages thatYou can sort of already do it in C by using designated
allow it. I am quite confident that this will never come to C.
It /might/ make it into C++, but as people have been writing
proposals to do so for 20 years at least, I am not holding my
breath.
initializers and macros
#define foo(A, B, C, ...) do{\
stuct technical technical={\
var1=default1,\
var2=default2,\
var3=default3 __VA_OPT__(,)\
__VA_ARGS__\
}\
actual_foo(A, B, C, technical.var1, technical.var2,
technical.var3)\ }while(0)
What an anquated language that this needs macros.
I don't suppose that somebody likes this macro stuff or takes it
seriously. It's just a PoC.
But at least we can imagine how named call arguments can be added to "antiquated language" if The Committee decides to do so. It would not
be easy, but clash with other language features is avoidable.
Not so with "modern language" that your like. For "modern language"
the clash with other [mis]features will be unavoidable and fatal.
[O.T.]
If I am not mistaken "modern language" is so twisted that it can't
even have proper designated initializers for struct (proper =
arbitrary order). I don't know what is the reason for that, but the
reason exists.
Am 04.07.2024 um 14:49 schrieb bart:
On 04/07/2024 12:12, Bonita Montero wrote:
Am 02.07.2024 um 18:53 schrieb Ivan Farlenkov:
I am a fan of being able to name parameters in languages that allowYou can sort of already do it in C by using designated initializers
it. I am quite confident that this will never come to C. It
/might/ make it into C++, but as people have been writing proposals
to do so for 20 years at least, I am not holding my breath.
and macros
#define foo(A, B, C, ...) do{\
stuct technical technical={\
var1=default1,\
var2=default2,\
var3=default3 __VA_OPT__(,)\
__VA_ARGS__\
}\
actual_foo(A, B, C, technical.var1, technical.var2,
technical.var3)\
}while(0)
What an anquated language that this needs macros.
How do you do it in C++?
With a inline-function that returns an auto to make the inner type accessible.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 150:21:45 |
Calls: | 10,383 |
Files: | 14,054 |
Messages: | 6,417,787 |