Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112263 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 80764 invoked from network); 20 Nov 2020 18:15:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Nov 2020 18:15:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 02DC0180533 for ; Fri, 20 Nov 2020 09:40:24 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 20 Nov 2020 09:40:23 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 1C29CF7E for ; Fri, 20 Nov 2020 12:40:21 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Fri, 20 Nov 2020 12:40:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=sI8OpE X/GvX9fxVir3v+xMlY4MmnLno4AzS6ldK95sE=; b=nmwRgZuZbdNCL8er0PGGi3 xCDPDxrpM69PXTpdu5eFlTrMQ5KAsbUpYl2Oaj+dmhpwYmQK6913ZrKpTHE7yNEk l5N86UoqZ9SQXHG31EU2qdDkf1vLwjyFiTXK+x6E6F1+p2/hoeNvNDT5Q+IR/SR8 GvLwmXFNRkiMh82z83a1vQMmctmCBJBzDo7g1ckdwbqbM7BEflMJ1emKeMdxxGz3 HEipfdYrth83lfl9jN0pykvIYbQHbvjW8bbBreprH5ccWtoI6LtdqMdKPiKcxgds 3jmDMMxSg7vWktIdyX+TD/Hfv4vZU9oQkaYkGuDOvfbPhITr4YzL/dP+MaTJJ5Vg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegtddguddtudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhephfeuvdfgkedvtefgheehkeevteefkeffvdfgveeh teeujeefkeegleeghfevhfefnecuffhomhgrihhnpehmohhvrggslhgvrdgrshenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehg rghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 6E85914200A2; Fri, 20 Nov 2020 12:40:20 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-622-g4a97c0b-fm-20201115.001-g4a97c0b3 Mime-Version: 1.0 Message-ID: <2369a5a3-3309-4131-81c1-4ebded96e61f@www.fastmail.com> In-Reply-To: References: <55e200cf-ed16-e935-4e09-728ce3362cba@gmx.de> Date: Fri, 20 Nov 2020 11:39:58 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Draft - Closure self reference From: larry@garfieldtech.com ("Larry Garfield") On Fri, Nov 20, 2020, at 10:01 AM, David Rodrigues wrote: > > 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. I kind of like that option, too. The main question in my mind is whether that means the same variable is used internally and externally or not. That is, my knee-jerk reaction when reading that was to ask if this would be confusing: $fib = function $f(int $n): int { ... } Viz, do $fib and $f need to match? It looks from the example below like you're saying it's actually: function $fib(int $n): int { ... } Where $fib is then both the internal and external variable name. That seems unexpected. It may be the best way to go about it, but it feels surprising as a user space developer. I do think something like that is probably the best way forward, though. The other question of course is short-lambdas. Would that mean this? fn $fib(int $n): int => match($n) { 0 => 0, 1 => 1, default => $fib(n - 1) + $fib($n - 2), }; If the function is inlined, does that mean the variable name leaks and thus remains in scope (and the closure thus using memory)? array_map(fn $fib(int $n): int => match($n) { 0 => 0, 1 => 1, default => $fib(n - 1) + $fib($n - 2), }, range(1, 6)); print $fib(5); // Does this work? (No you shouldn't write it like that, but since Fibonacci is the example we're working with...) --Larry Garfield