On Tue, 5 May 2020 at 18:19, Marco Pivetta <ocram...@gmail.com> wrote:

>
> I do in fact consider the `ParamNode` example in that API to be a bad
> candidate for named parameters, since I'd make all of the arguments in that
> signature required anyway: defaults aren't meaningful anyway, as the AST
> library where it comes from has all the context to instantiate them all
> (the caller, being the parser, will have all the parameters anyway).
>



Parameters don't need to be optional to benefit from named syntax; they
just need to be hard to parse by a human writing (and reading!) the call.

An obvious example is PHP's needle/haystack inconsistencies that everyone
loves to hate. If all parameters were named, strpos(haystack: $content,
needle: 'hello') would be fine and I wouldn't have to look up in the manual
to check I had them in the right order.


An interesting example is network protocols originating from contexts where
parameters are named.

Take AMQP, the messaging protocol used by RabbitMQ. In PHP, you would
currently write something like this to declare a queue in no-wait mode:

$channel->queue_declare('hello', false, true, false, false, true);

In Python (or Ruby, or C#, or Elixir, or...) you can make it much more
readable *even when providing all the arguments*, and don't have to
remember what order things go in:

channel.queue_declare(queue='hello', durable=True, nowait=True,
passive=False, exclusive=False, auto_delete=False)

Those aren't arbitrary names chosen by the Python client, either; they're
listed in the protocol spec:
https://www.rabbitmq.com/amqp-0-9-1-quickref.html#queue.declare

queue.declare(short reserved-1, queue-name queue, bit passive, bit durable,
bit exclusive, bit auto-delete, no-wait no-wait, table arguments) ➔
declare-ok




Incidentally, this leads to the one part of John Bafford's counter-proposal
I would be fundamentally opposed to:

> Parameters are also positional. foo(a:..., b: ...) and foo(b: ..., a:
...) are two completely different functions, by name. If you have foo(a:b:)
defined, then you cannot call it as foo(b: ..., a: ...).

Re-ordering is, IMO, a key advantage of named parameters, so banning it
feels both arbitrary and counter-productive.



Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to