Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112886 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34406 invoked from network); 14 Jan 2021 19:17:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Jan 2021 19:17:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DF6931804A7 for ; Thu, 14 Jan 2021 10:56:23 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 14 Jan 2021 10:56:23 -0800 (PST) Received: by mail-io1-f45.google.com with SMTP id n4so13239449iow.12 for ; Thu, 14 Jan 2021 10:56:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=5om/YNN86PbnpWIyHhR8qu6PtJMvv+kZywJnZhn4k28=; b=GO26a7dgHyfTD8wmm1OGG/dIntCTWzHC72PTaF2WMPleNzydYVJjs8+VZueKdKGW/X 5tSlxYu0UTq5k8i+b2ZdaZNS03Ol9OGWjHODI+CcAaG9BBxfXD0W1NZjlB2/SMrVjjWi q/SwRRzv3BWReVY5WSkAP1GGQjjeX3IX8e6gJKyQMPG/lxTPA6qjnduLNs4+Zi9R/B8I 25ITkLX97Jh9hrNpe2fC9KGrMoF/E8Nr9Fz0c5W3wMgS2CXU8aX8xPy0tho9DVSBy2Co dsC1zXcdlnhJdtpNyIu006GT5P2EBEfDb13h5akXYmIgeZHO3L8+O0EZjUyyeTd977RU 4C8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=5om/YNN86PbnpWIyHhR8qu6PtJMvv+kZywJnZhn4k28=; b=E08DOviQ5gk6E34g6DXZEjvOIoTUkLSOBKBqwkXcWwDmoXxd3z17an9/wRvCEt2Cks bbZFyU6Ehbdi1zDKXgIc+WLKPyXBxa4zzW271NuYJi2+oepTOZqFN4lU1jNr92FD+q+n 5Uc6HPZQuN0j+Nac+cTTVZxbkdLa+kzFmecdbA/Fd6d5X+bVtt7OQ5Tgf+VyrqxP8dgy 1WnJKjVfbiLWc/YFRIo7iLqcjlL+bbDHvPnss/vF/KgM5AJtXN94dU9S0MJfQNvtHVki dxSJlBhyDjxDrerFk5Nhpo80pDfP9gJ3AxJ6fOleyWDb3XUM+Nn0eSMzB9PUkZ+E5oxq t0Hw== X-Gm-Message-State: AOAM531jVB9ErIy/qTpD9uZwLp7Bl/5HAm7wLpgdZfyewdWOQ8+1pYc9 Y+8WcZFrAjXF9CcQhBmO5/aCFeQyqvnDx5t2KhnKudba3U99qw== X-Google-Smtp-Source: ABdhPJxZaCLCUIRRTvtCMsZvtT34XH6VImqyi/1UfPOy+3fi+CJ2nOXlTGbkTTma4SEfqlHJZSJxHVu3utHnNv/cr7I= X-Received: by 2002:a92:5b82:: with SMTP id c2mr3075242ilg.289.1610650582278; Thu, 14 Jan 2021 10:56:22 -0800 (PST) MIME-Version: 1.0 References: <31e07ff9-3573-cd55-188d-5ec9f99071ff@allenjb.me.uk> In-Reply-To: <31e07ff9-3573-cd55-188d-5ec9f99071ff@allenjb.me.uk> Date: Thu, 14 Jan 2021 12:56:06 -0600 Message-ID: To: AllenJB Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000d0687805b8e0cf4f" Subject: Re: [PHP-DEV] Feature Proposal From: browner12@gmail.com (Andrew Brown) --000000000000d0687805b8e0cf4f Content-Type: text/plain; charset="UTF-8" Thanks for the feedback. Thanks for the tip on avoiding embedded images. *Anna*: Definitely agree that long signatures, along with lots of optional parameters can be signs of other issues. That's why I used a 3 parameter signature in my example, where it's "required", "optional", "optional", which I feel is pretty common. Here's an example from the Laravel codebase of that in action: public function ensureDirectoryExists($path, $mode = 0755, $recursive = true) { if (! $this->isDirectory($path)) { $this->makeDirectory($path, $mode, $recursive); } } In this scenario, I may not particularly care to explicitly define my mode, and instead defer to the package maintainer, but I may care to prevent a recursive directory from being made. *Allen*: Thanks for the links to the other discussions. I think this RFC ( https://wiki.php.net/rfc/skipparams) is basically exactly what I'm proposing. I know named arguments are a hotly contested feature right now, and honestly I'm not sure where I fall on them right now. But I would also see this as a complement to named parameters. I can see this feature has been debated heavily in the past, so I probably won't get anywhere further than they already have with it. Thanks for the help! On Thu, Jan 14, 2021 at 12:25 PM AllenJB wrote: > Hi, > > It looks like your images have broken (Random guess: the list may remove > attachments). > > As a general rule, I would suggest avoiding screenshots for code. Common > mailing list etiquette for development lists is to avoid attachments or > embedded remote images as these tend to get lost in history, and not all > list members may be using graphical clients. > > Looking at what I can see of your proposal, PHP already supports default > values for arguments: > > https://www.php.net/manual/en/functions.arguments.php#functions.arguments.default > > ...and as of PHP 8 this can be combined with named arguments to allow > "skipping" when calling: > > https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments > > https://3v4l.org/rUGuP > > function test($foo, $bar = "bardefault", $qux = "quxdefault") > { > return [$foo, $bar, $qux]; > } > > var_dump(test(foo: "foovalue", qux: "quxvalue")); > > This doesn't allow for declaring required parameters after optional ones > in the functional declaration, but does allow for flexibility when > calling while making it explicit which parameters you actually want to > pass. > > While some time ago, this feature suggestion has been discussed before: > * https://externals.io/message/105123 > * https://externals.io/message/80201 > > (There were additional older threads I found via externals.io searches > for "default keyword" and "parameter skip" which I've not included here) > > You may also want to check threads / the RFC related to named parameters > as there may be additional discussion there. > > > AllenJB > > > On 14/01/2021 17:52, Andrew Brown wrote: > > This is my first foray into PHP internals, so please let me know if > > I'm doing something wrong. Currently just following the instructions > > from https://wiki.php.net/rfc/howto . > > > > Currently this proposal is only a "concept". > > > > I propose we add a "default" keyword that can be passed as an argument > > into a function/method call. This would allow the calling code to > > defer to the function signature for the value of a parameter. > > > > Traditionally, all optional parameters are placed towards the end of > > the function parameter list, but as we all know, sometimes this order > > can be tricky. So if some calling code wants to override a later > > parameter, it currently needs to pass a value to the earlier optional > > parameters. > > > > A current solution around this is to define all defaults in the body > > of the function rather than in the signature. > > > > Screen-Shot-2021-01-14-at-11.45.09-AM.jpg > > > > However, this adds some extra boilerplate, and we can't use default > > parameters as they were really intended. > > > > My proposal is to add a new `default` keyword to be passed as an > argument. > > > > Screen-Shot-2021-01-14-at-11.44.57-AM.jpg > > > > The first call of the function here is how we currently have to do > > things. We have to explicitly pass `true` in as the second parameter. > > > > However, in the second call, we now use the new `default` keyword, > > which will defer to the function signature for the default value of > > `$param2`. > > > > Thanks all, looking forward to some feedback! > > > > -- > > Andrew M. Brown > -- Andrew M. Brown --000000000000d0687805b8e0cf4f--