Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112262 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73129 invoked from network); 20 Nov 2020 16:37:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Nov 2020 16:37:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BBEB7180502 for ; Fri, 20 Nov 2020 08:01:57 -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,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-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (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 ; Fri, 20 Nov 2020 08:01:57 -0800 (PST) Received: by mail-oi1-f171.google.com with SMTP id d9so10968979oib.3 for ; Fri, 20 Nov 2020 08:01:57 -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; bh=fB/0HjcHSo/ksyY0jxGHWCykjvFr4YJuY9mtBe/tls8=; b=a09LcZnxRZhtiyXzscZznk47+is91aU1hbxzBIb/Ex7ix0NX8LuMDVQSAZ8CIktN0N c7ZPAXCEkc5lg62qUlOzm22ZeyyS5VXZ6H7/Naclx0Z51WtD0WIAPc1tpL+AqlDXhXOt HYGV5GLGjDZDfEiX7vF2eudGFjmeonVYI+cBghkz+Ej6bxzJE1M9+jqftNKjrkVFwP2n 2HCMdavzLIi3DFEyyzEVEQ8gd8LcIoyFwZxbJ614SMY3fqCNxofujIqugvYV4BpHjwwf BOEn1z0kOZNvM/6fwzUuyPANIjlKuPtpx3TNnXjLdcV7o1JB+0W1SinlkKA/3L2kE33W YF9Q== 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; bh=fB/0HjcHSo/ksyY0jxGHWCykjvFr4YJuY9mtBe/tls8=; b=GbwqrehPyzeEoSjFdbXtqW1ddluwpqgXzg9RwIuY86g01tD0/aJBOYQl4ZuTC85zZx R7mRI5QoKiD2ftBpKjj6m1gu8/NrLnABrqG9suVQK6s9fRm+LD9vZDxK33iA16aR9Ht/ Ehxz7houzIdSmu/1/QcvWSRhHYOXizIcIuC5uAq+n8UNC8VhFj7kBjRwtaFDGxdg39Bf 5MAtp1OEuoijdcJjRq9oyW+9IL48+ITD6zY5cZFTmKMGj/BxBAW6CJ7HMAxQ/n+6GVll 31Vb8JEvLgn7UwLsa3H2gMHR929ZEkjSEQjmypPF7KyVO7j11+PXG0bIHZFyXlol3nY9 36/w== X-Gm-Message-State: AOAM5329u0WfAFPU+LMC48e0xuEWgoM4edLyVsgLSpoHEQpaeHGwn7dQ FbkbY7C5VwUq9wBe4zbnoveqi3YQoZL6HU+EDN3OkCpU3b5IVQ== X-Google-Smtp-Source: ABdhPJxPx+z/9VJt7O6boTZL8gWT/ONYWHVDptbkaE1LSH7ruibPI0lBxSnha01tArr7F9Olf2b8g8rPv55UHhP2lT8= X-Received: by 2002:aca:500d:: with SMTP id e13mr1009287oib.112.1605888112228; Fri, 20 Nov 2020 08:01:52 -0800 (PST) MIME-Version: 1.0 References: <55e200cf-ed16-e935-4e09-728ce3362cba@gmx.de> In-Reply-To: Date: Fri, 20 Nov 2020 13:01:39 -0300 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000007a7cba05b48bf6fb" Subject: Re: [PHP-DEV] [RFC] Draft - Closure self reference From: david.proweb@gmail.com (David Rodrigues) --0000000000007a7cba05b48bf6fb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > Is it `returnType as $thing` ? That doesn't read well, but I don't see us making return type movable. as/use should be placable in either order, but maybe we give them required order anyway? I don't know if that would be a problem, because today we can have it "function(): Type use($x)", so "Type use($x)"? I have to agree that it may sound strange at first, but I also can't imagine many ways of doing things differently without creating more complexities. (1) function as $lambda (... $args): returnType use ($captures...) {} (2) function (... $args) as $lambda: returnType use ($captures...) {} (3) function (... $args): returnType as $lambda use ($captures...) {} // original suggestion (4) function (... $args): returnType use ($captures...) as $lambda {} (4.b) function (... $args): returnType as $lambda {} // without use() will be same as (3) Or, define a "inline function" with its own name, visible only within the scope itself. Similar to how JS works. (5) function $lambda(... $args): returnType use ($captures...) {} To be honest, as I typed what came to mind, I ended up preferring this last option. JS way: function fb(n) { if (n =3D=3D=3D 0) return 0; if (n =3D=3D=3D 1) return 1; return fb(n - 1) + fb(n - 2); } PHP (self-referenced) way: function $fb($n): int { if ($n =3D=3D=3D 0) return 0; if ($n =3D=3D=3D 1) return 1; return $fb($n - 1) + $fb($n - 2); } Currently PHP will accepts the same JS solution, but it will exposes fb() to global scope and could causes errors: function test() { function fb($n): int { if ($n =3D=3D=3D 0) return 0; if ($n =3D=3D=3D 1) return 1; return fb($n - 1) + fb($n - 2); } var_dump(fb(10)); } test(); test(); // Fatal error: Cannot redeclare fb() Atenciosamente, David Rodrigues Em sex., 20 de nov. de 2020 =C3=A0s 12:14, Sara Golemon escreveu: > On Wed, Nov 11, 2020 at 12:37 PM 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) ]; >> }; >> } >> >> > My initial reaction to this is: Technically doable (easy even), but look= s > a bit... ugly? weird? surprising? > > I think my main objection to it is how much is now stacked after the > parameter list: > > function($args...) : returnType as $local use ($captures...) { > ... > }; > > That's a lot of... stuff (granted, we have most of it already) and how it > looks when stacked together gets funky. > > Is it `returnType as $thing` ? That doesn't read well, but I don't see u= s > making return type movable. as/use should be placable in either order, b= ut > maybe we give them required order anyway? > > TLDR; Long winded way to say I'm not inherently against this, and I like > the idea of a lambda being able to recurse (not that I can ever recall > having use for it, but maybe I fell back on named functions for that), I > just think it's getting a bit wordy. > > -Sara > --0000000000007a7cba05b48bf6fb--