Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112227 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81101 invoked from network); 11 Nov 2020 19:45:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Nov 2020 19:45:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 45BF7180545 for ; Wed, 11 Nov 2020 11:07:41 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 ; Wed, 11 Nov 2020 11:07:40 -0800 (PST) Received: by mail-wr1-f53.google.com with SMTP id p8so3598146wrx.5 for ; Wed, 11 Nov 2020 11:07:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=hSNpBh2oHGxXlPQVjVjU3r3cs5igrA0LFTh8T8wQ86o=; b=QuqJSZmb1HxAlJ4Tsn/RDHz/MXYQ9qKa3crcQJj02qSt0S/mJYu2150DyL9ZhqSkjO oVTE9Ykrj69mLj3AGxr1LSijATXcqsKyCcvm+DKU0Cd9+LWFbx5m1UFWne/qs8t8DuUW 4mYOY4rl9zvY636ZQWfHA3rLjyfG5ivtAdA/4EQqitUTne+tBH29MyD4mF1scc3jimge 1mMJkQaFJABkrgOUbzzRXV6tP55KP2p8t/Oa5BHgitl7Pc26ZLmktTZua1b20qYgKeqs TblRC3h5Uwa/TArYCoqQHbHQisND4gQoErCHlyJyRFthS1s1JKjOwg0ao7trpNBfHF28 qAzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=hSNpBh2oHGxXlPQVjVjU3r3cs5igrA0LFTh8T8wQ86o=; b=dvWSW2FS6UCpOjDangrhl1ApR+Dtrwhim40Xp2UKEYRXK7zExPrHsqbwC4r6DildAc svFQNJF7LKTt3YCp8AgA+PR8QDFXGR8yvngot/hzUjdxS7HYgGcxvHY9ofRXUhXUrij/ J8Fwdlp+tGeEUHBdWqDxBTmIMmJt0eW7wId1iqsBWptw0/eOBtkp31+58Rh70imtSHqz m73EPtFTaeGzwwqqRFcpew/30ve4WQsvv+GaFyMn0/8oprGczytP0sbyBFvAbVvCQL/2 3/dJiiPZCGGdPzzTnB26Rix/r4eWKkg1rFPM4OjhrcLuQlGEmdjHsY/fdcdSw0qg3CZi Gn0g== X-Gm-Message-State: AOAM533ust8ea+RjHF/XZibwjNcXxcPAKHjd2H3CzdXsPq+oxzkgjix6 27uHSVkJoGQzxYehYYNjlwMoT1YrfUA= X-Google-Smtp-Source: ABdhPJxL26HojARrsz8Uk+ZpypnP6Qr2hfdqhtSVMJQsMaZXbleU4Y913PJs9FsLwWe98DYgTsnCqg== X-Received: by 2002:adf:9104:: with SMTP id j4mr18774518wrj.198.1605121658579; Wed, 11 Nov 2020 11:07:38 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id h4sm3469438wrq.3.2020.11.11.11.07.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Nov 2020 11:07:37 -0800 (PST) To: internals@lists.php.net References: <55e200cf-ed16-e935-4e09-728ce3362cba@gmx.de> Message-ID: <48712bc4-a7fd-0d74-e11c-7e2f1d04f827@gmail.com> Date: Wed, 11 Nov 2020 19:07:34 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Draft - Closure self reference From: rowan.collins@gmail.com (Rowan Tommins) On 11/11/2020 17:59, Christoph M. Becker wrote: > In JavaScript, a named function expression is different to a function > declaration: > > var fn = function foo() {console.log('blah')} > foo() > => Uncaught ReferenceError: foo is not defined > > vs. > > function foo() {console.log('blah')} > foo() > => blah > > So the named function expression is still an anonymous function; the > label is only defined inside of the function body. I think the key difference is that in JS, functions and variables occupy the same namespace, so "function foo" and "var foo = function" mean roughly the same thing. For the same reason, *inside* a named function expression, it acts as though there is a local variable with the given name: var fn = function foo() {console.log(typeof foo)}; fn(); // outputs 'function' which actually makes it very similar to... On 11/11/2020 18:37, David Rodrigues wrote: > My suggestion is to reuse the keyword `as` to set a variable that will > represent its own closure. It is more flexible once that we could choose > any name and reuse in nested closures. It is pretty similar to how SQL > works too. > > function fn1() as $lambda1 { > return function() as $lambda2 use ($lambda1) { > return [ gettype($lambda1), gettype($lambda2) ]; > }; > } I like this idea; it avoids reserving a magic variable name, but gives all the flexibility of having the full Closure object available (having a "constant" whose value was a Closure object would be somewhat odd). To take Dan's example from the RFC, this would no longer have any surprises: $fibonacci = function (int $n) as $fibonacci {     if ($n === 0) return 0;     if ($n === 1) return 1;     return $fibonacci($n-1) + $fibonacci($n-2); }; $this_is_the_original_fibonacci = $fibonacci; $fibonacci = function (int $n) { return rand(0, $n); }; echo $this_is_the_original_fibonacci(10). "\n"; The $fibonacci inside the closure would be a normal local variable, which just happens to have the same name as the one outside, so assigning to one would have no effect on the other. Regards, -- Rowan Tommins (né Collins) [IMSoP]