Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124629 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 7BC951A00B7 for ; Fri, 26 Jul 2024 21:54:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1722030944; bh=m5ODC35PJsmgAvBcoBNXYaU6geYSWgbieX1iRf0haRs=; h=Date:To:From:Subject:From; b=bBM8zVMc/t1OFZe+lq83kmucpKuiSzpMpYBHTwB28z22VJWWz+DDu9j62NN4mj5qJ heo9D6NOIrwfHungo/5K+jQmIHXpCsdv7SUppBE4VMFAU1IMMKZ89XGob4ybQtmcZs sCUtkiJbvj9LV3bz0FZAZm81Z9NYlQu6otelYzvN350UHvQRgIuyNngdLiIcov9p85 V4uZH2vtu2pxcxxtKzmfRMir8FoH4hrqg4syJ/FG6MPoYV8tcYRC3iKmz4v5BXYMBT 4p1QY7gvrbVXiE/LSLPVeZm2wRDVxAcXCCZ27shZ9NzwB7iUs0JTNlAWYhFgJ7I+FP hL7GWFj9aL3fQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 02C5E180068 for ; Fri, 26 Jul 2024 21:55:44 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 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 21:55:43 +0000 (UTC) Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-42726d6eca5so759695e9.1 for ; Fri, 26 Jul 2024 14:54:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scriptfusion-com.20230601.gappssmtp.com; s=20230601; t=1722030847; x=1722635647; darn=lists.php.net; h=subject:from:content-language:to:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=OfFkBCSx14AcS0i+fGuYNlDWduLGu0AHG0kNw2zFBQ8=; b=s7MhWTGYoPTwZyAO64JHDsubC/CbU04w8UMMcXVcquBo/7aiVkBZWDUT2eJEg7+7hL /AA6+NGgW9ikAxSzPys9lGgoKQy+u1hvJwsLFo6ivTpXi4ospvKldtUqfNdAq9BGX7cs JunaVkUPFp4eCXyeXMRzqfkYAJcoBmdkHY6d+xq81fzsm/LxO/DoCWszIkqGp3ywd+Ll SCdleKEP7s3Z1Ufu4p6MVhFL4D2s06E1xSlqViTQFFdIrvr5oFeqzeEDgLnPAQnjJK+P r7eHeZkw8gQBf9O5r4hEC+nDy4B95MzVqCHqPDvjuD2UINfdhEOV9byWiWpjvGry4wR/ AEmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722030847; x=1722635647; h=subject:from:content-language:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OfFkBCSx14AcS0i+fGuYNlDWduLGu0AHG0kNw2zFBQ8=; b=l6HaSAyeovg9QIesLPVx8PrM3vdAtupXHky5vmKlzBEk6HBXqcY5FP10x312V/IfuP vwRoKzz7rpKhQL5nmIKScpAT93rlREWdyFvZn8bxZcmyU+fd8GZYQGcXjDIGm3YDOTrh SFQq1pgLAIVigTEO0fLMN/AFVAos56oZAP6HcECIoy/x+0GCkvq9P5f6g5PS+v8et3OL B/I6yvOYijjQmgDz2vvBlA2H7yqWrXHQWQuW9tyHAp2LjKFQBDWTHgkjsWGzutdy7Cts WLgD6Pf5VwdG/tsFzHOEvdmbVa3REpu9HOOJ4f6YPU3jqtcK4GgoR1MhoikhN6o+5k9N oO9A== X-Gm-Message-State: AOJu0YxrzM7S3fIxKKMqtTNcIPVppzWRYhnLJeo4Pwgr0Wkx7Oc2kn35 KNG2ZmDZuWqzr8m1ZvW9Im4WK7+oR9VnXY2/02LSd4V6mhLCsAGanTKtLg24KVMcC9FnbJL2DMs s X-Google-Smtp-Source: AGHT+IGSYnVO6qEF8VuqFJTT207z97y9XgZCywp4gf9eUuxz3CTBFxqth0rGZv8h4wGN03vR4HFI6g== X-Received: by 2002:a05:600c:3108:b0:426:4765:16f7 with SMTP id 5b1f17b1804b1-42811d9daf3mr5100475e9.21.1722030846698; Fri, 26 Jul 2024 14:54:06 -0700 (PDT) Received: from ?IPV6:2a01:4b00:bf09:5101:ac04:482c:4574:e3be? ([2a01:4b00:bf09:5101:ac04:482c:4574:e3be]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-428057a69c4sm92717885e9.33.2024.07.26.14.54.06 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Jul 2024 14:54:06 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------XX4qtaJlZRO3BkNvszerVY0G" Message-ID: Date: Fri, 26 Jul 2024 22:54:05 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: internals@lists.php.net Content-Language: en-GB Subject: [PHP-DEV] Explicit callee defaults From: bilge@scriptfusion.com (Bilge) This is a multi-part message in MIME format. --------------XX4qtaJlZRO3BkNvszerVY0G Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Internals, 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 = PHP_INT_MAX, int $offset = 0); function myQuery(string $sql, ?int $limit = null, ?int $offset = null) {     query(...array_filter(func_get_args(), fn ($arg) => $arg !== 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 = null, ?int $offset = null) {     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? Cheers, Bilge --------------XX4qtaJlZRO3BkNvszerVY0G Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi Internals,

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 = PHP_INT_MAX, int $offset = 0);

function myQuery(string $sql, ?int $limit = null, ?int $offset = null) {
    query(...array_filter(func_get_args(), fn ($arg) => $arg !== 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 = null, ?int $offset = null) {
    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?

Cheers,
Bilge

--------------XX4qtaJlZRO3BkNvszerVY0G--