Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126987 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 B2A3E1A00BC for ; Mon, 31 Mar 2025 21:35:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743456777; bh=Qe51YoPDRDHcvXIrfSLGeZOiqYAruCFOH777KxQLPEY=; h=Date:Subject:To:References:From:In-Reply-To:From; b=eeTcxEltHGv7u77gnvd5i2t2hVc2e0dJb4+alJTdLhPcMzdim0goRxeMvBEOyVl8S CLt0Iq9Qvd7/LnPdTBs88fPpGaXm6Y8tGxiUSk83k60IYnOzubpwJuo5jqB3bKgraa aoGdMKwwuWMAMu0B7rlFLCYR+sWn+T3oy/ZXi6i2wka5vBJjW2QOTRTDpYXGmzOdno 8pOKTQSGdwYOYjMM8pIvtZkyePGIn9jhFIPwUVpiwA8d+LA6tf2UWb8R0Co4a4SCc7 UpiQvn5l7sMjkVwTtU0qfMLkqgMelqGqKr03RTfLp7l+IU45VE6iPGfR1H7KHUevmh 6EJb3ZTd/x4rA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3B7E9180034 for ; Mon, 31 Mar 2025 21:32:56 +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=-3.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,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-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 ; Mon, 31 Mar 2025 21:32:56 +0000 (UTC) Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4394a0c65fcso46990115e9.1 for ; Mon, 31 Mar 2025 14:35:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scriptfusion-com.20230601.gappssmtp.com; s=20230601; t=1743456922; x=1744061722; darn=lists.php.net; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=qeveksMFTVXIvMINAl921c2L/BGo4SySIpHS+yTFt3A=; b=sLuMQj+AWqFhRpUJPQ0rpUQqWp3+QVYt1rbB11xR1g3/e5wJQN670whwkfSwfECYcX cUfi030CZuH7Sn4GRPU6nM3m8F8BDWnyJV8dX0lUnAsSzUk7Y7XL3wNYhR3ba+CAmO70 FtGB2CCCmp9Dpvp5G/QiaTVm54+i/+DV3sJm8banlLY0nqrW2UpMQiTwi1ILWCeOtB/w Hn50O8eGMjZBuzc21ljTaeM0r4Y0eqXbamRpvb5EfeR4YHZvj9oOdD0DmC6y4ZhSLY9Q aS8pYPst81i0BmS2ZLICbNHmUQLBtc5C8OoTdfo20iS+dgYTuipTwwZeeCTEK0VO3Ohe Y8zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743456922; x=1744061722; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qeveksMFTVXIvMINAl921c2L/BGo4SySIpHS+yTFt3A=; b=N86K6HbMObIi4YnEnJQlHQ4FvJTDwnPENuOHWT5QDG7plzhyDVb8so5PHOmpdczYk4 tNt4APKJrBDoZL/dl2gbTJYe/CbTJl+TiYDBuuwGAMxUxk3K+YID/tMgsfxySHGKLL5N pfMWROMSM81gzgfsZhSJapscf+yMfRKQfvMOBZknqI479Z9bmc9YfTCqV31yHO1G1nux tVfUKHsvQDFXyrPgE5XpUppFVwNnHDy2zL/qk9jLq+lANHXOnVuuF8UeaG4NUcISUSDX k1TiGj7Ex8joh73aALQulAxSlT2JsQtelH5khLR2VDzHzCkahr5dWqHWqYrw+/Aqx9J2 ONng== X-Gm-Message-State: AOJu0YzqicxTj5O5zOunmzCQlR/1MjY9zqOrahMTqOHgkjFItaggwKga Dtii7ibqFgrt+FGEky/rmU+YY2iCEPI+6qpVrOM7ZLEeT8/05mRdQTzfW+DBssSD0QyOIjIxPQ+ v X-Gm-Gg: ASbGncul7T/WyIO88IMetnv6NnrvVc7ZhXtz78rOa7TWfpdOGz+1bypRzYzxGF5nfv9 U/AlGE2U4RDoqrdjfUDejrdvBf96qg7xY1jh0SWn8HOlHbBk08tdLBtUZn81G8KBjgy/4c9UKIU dcYNOJlJPYKMw9J3s7hoQvngoo2L+SXUaK979j9DGU2Lboli4pIEUvWZMOiK/3BkORZfrWAll4+ WzDPLX/NbqlP/XrwujGWkFloWI3VeScgwDi2qJV/0RN6fSp9NHC/CJv9wbgSaFYtBuHdDMuYX5V WB+QJXKEEqHccUywSZR1TbbQ1uNYz+cfvrtb2PbcSMMY60RCIzk4FXJozS0cF3rnHLdvlX9nodV VeQ385Cs/Rq/kOlqlnuHT0Arw+veh X-Google-Smtp-Source: AGHT+IHqMXE22WxUfHECfBBTfi0bpvsGy0m/70VPD2cBxv3braSSVAs8YgxYvJKSXYzwrsSOAbPAHA== X-Received: by 2002:a05:600c:4593:b0:43d:23fe:e8a6 with SMTP id 5b1f17b1804b1-43db61e037emr91629585e9.5.1743456921863; Mon, 31 Mar 2025 14:35:21 -0700 (PDT) Received: from ?IPV6:2a01:4b00:bf09:5101:3d81:13f1:de94:af56? ([2a01:4b00:bf09:5101:3d81:13f1:de94:af56]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-39c0b663617sm12410084f8f.34.2025.03.31.14.35.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Mar 2025 14:35:20 -0700 (PDT) Message-ID: <6828dba8-6ae7-4bac-8d7a-eb602d0b53a4@scriptfusion.com> Date: Mon, 31 Mar 2025 22:35:18 +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 Subject: Re: [PHP-DEV] Closure::getCurrent() for recursion To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit From: bilge@scriptfusion.com (Bilge) On 31/03/2025 21:38, Ilija Tovilo wrote: > Hi everyone > > A few years ago, an RFC was proposed to introduce special syntax for > self-referencing closures, mainly used for implementing recursion. > > https://wiki.php.net/rfc/closure_self_reference > > The proposed solution allows specifying a variable that will be > assigned the closure object on function entry. > > $fibonacci = function (int $n) as $fn { > if ($n === 0) return 0; > if ($n === 1) return 1; > return $fn($n-1) + $fn($n-2); > }; > > This is essentially already possible through a by-reference capture. > > $fibonacci = function (int $n) use (&$fibonacci) { ... }; > > This cannot be a by-value capture because by the time the closure is > created and variables are bound, $fibonacci is not assigned yet. The > downside of by-reference capturing is that if $fibonacci is > accidentally reassigned, the reference within the closure is also > changed. > > However, I consider a language change largely unnecessary. Instead, > this may be solved with a very simple static function: > Closure::getCurrent(). > > $fibonacci = function (int $n) { > if ($n === 0) return 0; > if ($n === 1) return 1; > > $self = Closure::getCurrent(); > return $self($n-1) + $self($n-2); > > // Or > return Closure::getCurrent()($n-1) + Closure::getCurrent()($n-2); > }; > > Another suggestion was to introduce a magic constant __CLOSURE__. > However, this would be misleading, given it can refer to different > closure instances, thus not actually being constant. > > You can find the implementation here: > https://github.com/php/php-src/pull/18167 > > I picked the name Closure::getCurrent() to be consistent with > Fiber::getCurrent(). Contrary to fibers, calling Closure::getCurrent() > from non-closures is nonsensical and thus disallowed, resulting in an > Error exception. It will only succeed when called directly from > closures. It will not recurse the call stack. > > Do you have thoughts or concerns with this solution? If not, I'm > planning to merge it into master in a few weeks. > > Ilija PHP now officially better than JavaScript, which had arguments.callee since inception, but was then deprecated without replacement.