Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124630 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id D28011A00B7 for ; Fri, 26 Jul 2024 22:42:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1722033866; bh=8VDHjB7HarDVilxUDYQM/bWfDdMomjUW1RUcQnyavrw=; h=Date:Subject:To:References:From:In-Reply-To:From; b=JILyQDmI+/Pi6CZ0EYULlFbaXfOKEBQYXpGM4zTORjF6HyymukMFJVzfUzffKcJ5u DwlqeDpFUx4JTBfg/rBE4JX8tmhywpK8IlGc+xcbZ7l2OQWl0fJTdYIVtlFfSEdWci iLbOeaiipsQ2dXFOxK+JqXGatkWqubb9tmxOmUEy26R5/DqcRhkRsWTsZhlq4rYSPb FXkEEbfEC+H97jzEqDeKSfIzdglXbN1uT40Zlkpne4whefCkT/5A9kZuBt105DTFtY SjJv+WR2xl5CpAiFo5H3vR7h+0HpE6SGZwmaX/DeboYRL0DuaK8O2hRc2OQTlpucyU 4CIIW6fw1yiTQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7AC64180039 for ; Fri, 26 Jul 2024 22:44:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 26 Jul 2024 22:44:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1722033767; x=1722638567; i=cmbecker69@gmx.de; bh=8VDHjB7HarDVilxUDYQM/bWfDdMomjUW1RUcQnyavrw=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=lV/M6+g+f21VdtCuyeurKoHDh5XUkN42R1fhi77/JvuUnwaWDCTDC29RaC6O8Oh8 RNME0Gwu6w9UOpDHleMdhsCyDvRfilGBuVPC2AUWKpsyxMZ0CC+z9VVd2KHOgT9qx 8obskW5S0elHOO4UjjyHFi18cEkiUDinwLdbXZtEIL9FWvwI87x94c9uS2fr+CWrQ t6zxRUjJrYxSoZ0ngLiE9iihfhBp8D6HdBajg9Zf35Vo2D41AdTalanILe35r0pNr YW4RXlaRirC3KPWh/VGKuQwxbn/9ZUy2pXY/rLlHH5mglWPzOreYE1Tbd9H5JWufb a6Uos0OVrArdQ894rA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.2.130] ([79.251.205.37]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MhU5R-1s3esP3z75-00chrb; Sat, 27 Jul 2024 00:42:46 +0200 Message-ID: <8ac3f087-a739-4759-891c-3dd19260ef5e@gmx.de> Date: Sat, 27 Jul 2024 00:42:47 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Explicit callee defaults Content-Language: de-DE To: Bilge , internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:JdPgZhovl3BOeVnP+jtwIh+cROTOwCJCA+8EUE+gyCyvLefuI8d 4ZiEP7qNY/bmQ9QcmUpTN8I3LRoKEJEwHHC7vyObdF3gLUM/Q0BCqjA7z7GXpBH9/eminQ1 0IfQoNLo5wI3O3U+RD6xjRbc6HiSy5/1KgV3qYQsVHHfbs3YyGy+BKtf8dwbq3bCu7osrH5 1tOuyoYskGAM5kjuZmtMw== UI-OutboundReport: notjunk:1;M01:P0:JuJeNUDpbbs=;UWz2pAJppIaRxMSLCNKzWcw8MZl 4q4BQsgxbNtGpxIj7y++3dNsFQ5mRbk5tZAmoXdj9V4XJ6fr9R8l1UgbGd46aQ7l6QaPwLb75 gnIxWW1Fd8EO2VluKuccewuSXbQqIIKG0ErCmX+nf+x7pO6XNYhfCYohhjbuPGA3nvjbTtVu0 nUj0RvpYoVqMOXfVFb8/1z8QS32zdaIOx25fKHVFvEAAY8ClSLxgZjZDkVVAlixIVqRKjj0gv 0ySYtYlBkVYZQj9vq9UkDO/YZKCz7nkA2CITzroZYVJoVjsGlW1rU8z2d4PaVnnjMCn+N9fOn //i+sOWElpBOk58SlZLQACHpktSewHnAuVDUNYlC1bUhYyL1D+VHJlemfLLfuQ2DOx7UUzWBv 512PBsuUpZadlf9hbJYrB5X7VB1+aPAwo47lEMwHJsgOu96jIOC9WGZEQ2FdpHRvIkPvSj/xZ 6iZERzcV/pjqcWNv46BuW5EkH5cMoQp/OjDXGKPtBBUhnfT7R3SjfkdcFn1O0mBXQkggdc8Ac S6TmSDm4XYCHFw8L2S7i5134muAwAlBKaFNIOmNtyxRW3kQ42aTiSQ9bF+oot2nYXLEPxdTmf rVLM/Cyn+oz/QzC1bD+GLRgIV8Xrn3RIh6VAQx35IGbSbynwxwXHAb46jmZoj2aHGYSjUZ4gH ibRonlbKh59YFXu7WREPmhHFGa+2N9bi1BLDmL+cLcUGOD9iqfPpdZwLBmpNvzHO4o5ryVXx9 DPNANQiEzOfUvwSFUkW0VPAU4j+s+qvf/Qodf2bm0SLzS+qq+XTNOH31wxCngnFDreO6cpUuT 2sO7XO55MAnQs6RHYL7uEQ9A== From: cmbecker69@gmx.de ("Christoph M. Becker") On 26.07.2024 at 23:54, Bilge wrote: > New RFC idea just dropped. When writing a function, we can specify > defaults for its parameters, and when calling a function we can leverage > those defaults /implicitly/ by not specifying those arguments or by > "jumping over" some of them using named parameters. However, we cannot > /explicitly/ use the defaults. But why would we want to? > > Sometimes we want to effectively /inherit/ the defaults of a function > we're essentially just proxying. One way to do that is copy and paste > the entire method signature, but if the defaults of the proxied method > change, we're now overriding them with our own, which is not what we > wanted to do. It is possible, in a roundabout way, to avoid specifying > the optional parameters by filtering them out (as shown in the next > example). The final possibility is to use reflection and literally query > the default value for each optional parameter, which is the most awkward > and verbose way to inherit defaults. > > Let's use a concrete example for clarity. > > function query(string $sql, int $limit =3D PHP_INT_MAX, int $offset =3D = 0); > > function myQuery(string $sql, ?int $limit =3D null, ?int $offset =3D nul= l) { > =C2=A0=C2=A0=C2=A0 query(...array_filter(func_get_args(), fn ($arg) =3D>= $arg !=3D=3D null)); > } > > In this way we are able to filter out the null arguments to inherit the > callee defaults, but this code is quite ugly. Moreover, it makes the > (sometimes invalid) assumption that we're able to use `null` for all the > optional arguments. > > In my new proposal for /explicit /callee defaults, it would be possible > to use the `default` keyword to expressly use the default value of the > callee in that argument position. For example, the above implementation > for myQuery() could be simplified to the following. > > function myQuery(string $sql, ?int $limit =3D null, ?int $offset =3D nul= l) { > =C2=A0=C2=A0=C2=A0 query($sql, $limit ?? default, $offset ?? default); > } > > Furthermore, it would also be possible to "jump over" optional > parameters /without/ using named parameters. > > json_decode($json, true, default, JSON_THROW_ON_ERROR); > > This proposal is built on the assumption that it is possible to specify > that PHP should only accept the `default` expression in method and > function call contexts. For example, it would not be valid to return > `default` from a function and substitute it that way; my proposal is to > only permit `default` in literal function calling contexts. My knowledge > of internals is insufficient (read: non-existent) to know whether or not > this restriction is possible to implement, but if it is, I think this is > a good idea. What do you think? I have only skimmed your suggestion, but it sounds quite similar to . Cheers, Christoph