Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117938 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49542 invoked from network); 13 Jun 2022 16:02:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jun 2022 16:02:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4B3CB1804C6 for ; Mon, 13 Jun 2022 10:49:23 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f44.google.com (mail-vs1-f44.google.com [209.85.217.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 13 Jun 2022 10:49:22 -0700 (PDT) Received: by mail-vs1-f44.google.com with SMTP id k4so6629053vsp.3 for ; Mon, 13 Jun 2022 10:49:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SJva0yqTUEf1GBuXwS2DU3Sa3nRns8rsGGMK4yfJcoc=; b=Agz6+sYKXu6o6bgTPR8sMcpg671Fu+Jjlt3FOGGfVHfa5OXg/dyZDtEmljs0iDnHUJ 0arJ1H6LukWClnX0w2PVWIdmW0Y41k/nFutt+lluxbWnD6As0jHAFZG1Ve9nr/XApuzl Nich2q4rB//0eefBhOxqZEdHDNWt3LdOi4uOcpKAy/fBBmxVT8h70HBbVRBaYSHUiGeh gZDmIuUPNJTvZPoCM07hYB1FFKBgfLz7DJZ6sVKwVySVdLNrmenCvj7AG7psegtMCTjz AbJMYl65XDUXn7W07m23RYrtRUEX9IeAJVMNJK+LE9eFWR65/UmzdLgQjLnQRQMQsWZe GqHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SJva0yqTUEf1GBuXwS2DU3Sa3nRns8rsGGMK4yfJcoc=; b=OHTBokBBtHC5Rk9o6hEEGaIJNGQ9uWSoj4FUcY0NOucquKmkFBtUj9fdDrnX5YoqYt 5fANzQdwC0twmN9mHoot0RWhCGYqTcvXEX+T3UVAazSBehDda61QTrJlBYGalGghj1Eh ipkTk4qRzHHGbvVnkCn3BCGkoRyPK1zaUeULUoKJfitGmbWStHYB1ZeIN4H19vmwT6ge JSNA6ACUjAFAFSWriNZJ2Nk0fMcluPuR4TNvhmMXe9ef7T6X/1JDAI9O18qTbef/ATrA HY3fZbz1auUYcQoh2K0kuxPKFggx1vmsByIrapeTJlkEuDOmp1Ea3VE7qfInzrEgn3M7 gB7Q== X-Gm-Message-State: AJIora89WwAkihxpcU5wlIdU6PXyy+/I1sjIhVazHsWWjFiKQfkaeP+3 Fyjx1F8UnkMefP+qq4nS+zG8stJV7pvukJRBnztMiw== X-Google-Smtp-Source: AGRyM1vhLtN3WrY3qlAl4XCU/ob2EobTA6WKHtoHfIqrrGb7uJLELstq6ALZdUCZojTplXRiaoxkfahrX185f6V3Wk4= X-Received: by 2002:a67:df16:0:b0:32c:bac6:95ad with SMTP id s22-20020a67df16000000b0032cbac695admr254766vsk.29.1655142562213; Mon, 13 Jun 2022 10:49:22 -0700 (PDT) MIME-Version: 1.0 References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> <2347345.PIDvDuAF1L@arnaud-t490> In-Reply-To: <2347345.PIDvDuAF1L@arnaud-t490> Date: Mon, 13 Jun 2022 18:49:10 +0100 Message-ID: To: Arnaud Le Blanc , Larry Garfield Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: Danack@basereality.com (Dan Ackroyd) Hi Larry, Arnaud, On Mon, 13 Jun 2022 at 13:57, Arnaud Le Blanc wrote: > > Auto-capture in PHP is by-value. This makes this impossible. It also makes > explicit declarations non-necessary and much less useful. > Separating off some pedantism from the hopefully constructive comment, I think some of the words in the RFC are a bit inaccurate: > A by-value capture means that it is not possible to modify any variables from the outer scope: > Because variables are bound by-value, the confusing behaviors often associated with closures do not exist. > Because variables are captured by-value, Short Closures can not have unintended side effects. Those statements are true for scalar values. They are not true for objects: class Foo { function __construct(public string $value) {} function __toString() { return $this->value; } } $a = new Foo('bar'); $f = fn() { $a->value = 'explicit scope is nice'; }; print $a; // prints "bar" $f(); print $a; // prints 'explicit scope is nice'; Yes, I know you can avoid these types of problems by avoiding mutability, and/or avoiding capturing variables that represent services, but sometimes those things are needed. When you are capturing objects that can have side effects, making that capture be explicit is quite nice (imo). I think the different emphasis on capturing scalar values or objects might come down to a difference in style of how different people use closures. cheers Dan Ack