Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98114 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34304 invoked from network); 2 Feb 2017 02:19:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2017 02:19:39 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain lerdorf.com designates 74.125.82.178 as permitted sender) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 74.125.82.178 mail-ot0-f178.google.com Received: from [74.125.82.178] ([74.125.82.178:34270] helo=mail-ot0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 33/A7-51557-93792985 for ; Wed, 01 Feb 2017 21:19:38 -0500 Received: by mail-ot0-f178.google.com with SMTP id f9so2063890otd.1 for ; Wed, 01 Feb 2017 18:19:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lerdorf-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=u9a3pk/XL0HTIKFQc8/jEAC4Ez6G9TUvFUUtpQFIqK4=; b=mwNLc2RCbbUKyw/UtHm+iOdMcVlSfwALiultAksXHYjTf58AINahpRFsAXo7yRGxz3 mD2fqr9yM/lvalqwCqXWYhRLrY+LN3rSe1/OhaWlfwhU0DKrDXSKyc2TiAGd9SILQRwd JZnpKVjMKi7bcMjf/qSDHrrZAO24JOVomQk1kE+vgBAlsNXbDcHXIgQK8c2z3iKHr0rV wLpJxE1Zg3cM9KNGBeiUEAdoifHO+qsiuyh9xoIK9IQXF8zBWFDmHaQNBPcSs/PAtNkB 6WSJtFfHZR5sIWdAs9i3OFqQ1aMlv3OM/QoCsgIc5kLy+FK1+vhOMLRAaeh19dBJPaZR z8+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=u9a3pk/XL0HTIKFQc8/jEAC4Ez6G9TUvFUUtpQFIqK4=; b=jX8V0vbBZWOlIFaoJX0UOkKW/fpZrAUaC/0O5bxfND6l7COMfa7Q3lLPyUove3u5kF kbDDZaMidna5L6fhQZ0Fg3AkUTQ4CIu6yqgGEBtKcotxJ4EoHCTlk73AAv/3GwWEY4nm Qystk6ONZRWPK7XKgC125GpOihmroh/RhCnGD1LH6S0Neb49Lq3hqq+SgDeOzduMTfne 0MCsb/vNAo+mf/0fYOSblMvG83YZ/m/Vt7LUhA2rArBCbejEjkVXcZHe+BwdykoCMymu QEEYvqs6BZJb0eTItwQoXc2gDk4NWNgdvY9AmZNMZ5/vzuop0eH7ieb4amotpWddnDnN ONfQ== X-Gm-Message-State: AIkVDXJ3lTZnec3A2vDqHpBUM8Q71ZXnk46coU4oo+8RgV7k6OfMAU345lx25jcR7oXjubM6Te74kmzx4t+SvQ== X-Received: by 10.157.14.114 with SMTP id n47mr3349934otd.29.1486001974455; Wed, 01 Feb 2017 18:19:34 -0800 (PST) MIME-Version: 1.0 Received: by 10.74.121.84 with HTTP; Wed, 1 Feb 2017 18:18:53 -0800 (PST) In-Reply-To: <68e8ffea-7df9-a3e5-23ac-0600c93d5986@gmail.com> References: <1E.BA.51557.966F0985@pb1.pair.com> <20170201160548.7qxyjhlnpfepwmgs@portable-ws> <68e8ffea-7df9-a3e5-23ac-0600c93d5986@gmail.com> Date: Wed, 1 Feb 2017 18:18:53 -0800 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113f0584d2d88f054782cbe8 Subject: Re: [PHP-DEV] Re: [RFC][Discuss] Arrow Functions From: rasmus@lerdorf.com (Rasmus Lerdorf) --001a113f0584d2d88f054782cbe8 Content-Type: text/plain; charset=UTF-8 On Wed, Feb 1, 2017 at 3:06 PM, Rowan Collins wrote: > On 01/02/2017 20:17, Rasmus Lerdorf wrote: > >> You probably think of $var as being a local scope variable here, >> but really it isn't. It is an imported variable that happened to not exist >> in the outer scope and it was assigned a value in the inner scope, but >> that >> assignment can't leak back out so it is still unassigned in the outer >> scope. >> > > I'm very confused by your terminology here. You definitely talk about two > distinct scopes, "the inner scope" and "the outer scope", but then you > assert that there is no "local scope". Surely the "inner scope" is "local" > to the closure? > > In actual fact, it's local to *each invocation* of the closure, because > the function is initialised with the original values each time. For > instance: > > $a = 1; > $fn = fn() => ++$a; > $a = 42; > echo $fn(), ' ', $fn, ' ', $fn(); > > This echoes 2 2 2, not 2 3 4, because the $a being incremented is new for > each invocation. Nor does it echo 43 43 43, because it is only the value of > $a that is captured, not its reference. > > I think the best description is this: the closure has its own scope, which > is initialised on each invocation with a copy of another scope taken when > it was defined. > > > To go back to Bruce's question, the original statement was: > > you don't typically need a local scope at all for these short closures >> > > The important point here is that within a single expression there is very > little you can meaningfully do with a local scope other than read from it. > It's generally a bad idea to alter the value of a variable and expect > another part of the same expression to see the new value, because you can't > always predict how the compiler will rewrite the expression (I think an > example of this came up on the list a while back, but can't think how to > search for it). > > So for most purposes you can consider all the automatically imported > variables to be immutable values, which makes their scope much less > relevant. It is mostly a semantic thing. Of course you have a local scope, but because of automatic import you don't have a clean local scope so it is not like the function-local scope people are used to. Like you said, it is a mostly immutable copy of the outer scope. But people need to understand that the immutability doesn't apply to class variables because there is no automatic clone done on outer scope objects. class C { static $var = 1; } $f = fn() => C::$var++; echo $f().$f().$f(); will output 123 and the value of C::$var would be 4 after this. -Rasmus --001a113f0584d2d88f054782cbe8--