C++ Logo

std-proposals

Advanced search

Re: Making parameter names clearer than func(true, false);

From: Tae Lim Kook <uruwi_at_[hidden]>
Date: Mon, 08 Jul 2019 19:26:46 +0000
On Monday, July 8, 2019 3:19 PM, Hyman Rosen via Std-Proposals <std-proposals_at_[hidden]> wrote:

> Posix system calls, an API that isn't about to change and doesn't care whether you
> think it's good or bad:
> ssize_t int write(int fd, void *buf, size_t count);
>
> Is the code
> ssize_t written = write(2, "a", 1);
> writing 1 byte to file descriptor 2, or 2 bytes to file descriptor 1?
>
> We should be able to say
> ssize_t written = write(buf => "a", count => 1, fd => 2);

I don't think this is a good example -- if we enable named
parameters by default, then parameter names would become part
of every API, while if we make them opt-in, then POSIX
wouldn't be able to take advantage of them anyway without
changing its API. Besides, Thiago asked for good APIs, not
APIs that are unlikely to change (and I'd add APIs that are
bad but couldn't be comparably improved by existing C++
features).

I think memcpy (or its more idiomatic cousin, std::copy_n)
would be a better example here.

Orthogonally to the good/bad API debate, I think the syntax
for named parameters should be:

void foo(int bar); // positional-only parameter for comparison
void foo(int .bar); // can only be passed with name
void foo(int .?bar); // positional or named

and for calls:

foo(.bar = 3);

to mirror the designated initialiser syntax.

-- T.

Received on 2019-07-08 14:28:52