Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94472 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83757 invoked from network); 11 Jul 2016 17:55:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2016 17:55:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=michal.brzuchalski@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=michal.brzuchalski@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.178 as permitted sender) X-PHP-List-Original-Sender: michal.brzuchalski@gmail.com X-Host-Fingerprint: 209.85.220.178 mail-qk0-f178.google.com Received: from [209.85.220.178] ([209.85.220.178:36713] helo=mail-qk0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8B/00-17655-28DD3875 for ; Mon, 11 Jul 2016 13:55:15 -0400 Received: by mail-qk0-f178.google.com with SMTP id 82so98996443qko.3 for ; Mon, 11 Jul 2016 10:55:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=OHpjF9hfMmL5piN8vnXAtnjw0YU4zbviExLzqZizsMo=; b=RQHGvCW1hYlaWYSyxyaIBgwVe5JqBeVAiRAQc9kcqTpYzm7gzU6kiLLogePLvDyQKS XnDoz/trC9n17X30pa3AzPrYan6ATwAtUVFhKXXiViif7fC0LVrdz9chEIZTiG9WZb5c TpyChC+v2iXbvuaAd3WNPzd2UICnK4YHg5R8V5k2ZAUbabQdPCC3B1F11BCZt7wEFTTs ShxrDY85h5UUincc1s5cbAawWXiDNNh3KEVqwzZOHc80X97IUlcYXqPbskUD2SkpUtdE QOgDqA1aV/LMWkK3inkI2uf9sBtHg49OjFkfLfAoN0NymaKDx8tEkhilQgm+WXSZwR5H FOpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=OHpjF9hfMmL5piN8vnXAtnjw0YU4zbviExLzqZizsMo=; b=ACtdh2AjgA8SzfPsFGF85GX84XKoMUReHPLRU//Br5rqhl4L3QE9rKype8iQqlQSvD NeXf/KrG4HTpsQnj3ZEs8h2P8OzzUSD9ZlCMaAN+cN2brPL5Z+6Eqj0NXG3/7gnhyzkU L7jlZUU3QKQxejIm6S0AzcWMh9jnKcKQiFABQtacG7YT7E+Y7SMrCciYNuKB5u/c3cD9 /Zl67DTJa+6asPBvJT6qKV0iEJSk6r3//sAQM/RlWjITEKWu5Za/au7+orr6Kf4YYikN YZlMh/CMRX15ETQ9ImZq6HfezgClg/tHQq6MXdJUPGXuO3RjzO6cda2c7kUDvbdThUyi GXJA== X-Gm-Message-State: ALyK8tLfepstezIT3r3qA5LtyQzoIh9lpOjwQD/NiWVkCgOtXa4XquNfBCRGdDWcnN+rlQpri6Gm9E/OrsJtTw== MIME-Version: 1.0 X-Received: by 10.55.158.18 with SMTP id h18mr26217155qke.161.1468259712259; Mon, 11 Jul 2016 10:55:12 -0700 (PDT) Received: by 10.237.53.155 with HTTP; Mon, 11 Jul 2016 10:55:11 -0700 (PDT) Received: by 10.237.53.155 with HTTP; Mon, 11 Jul 2016 10:55:11 -0700 (PDT) In-Reply-To: References: <86455a1e-eb57-1f30-9016-ac70c9f30bdd@gmail.com> Date: Mon, 11 Jul 2016 19:55:11 +0200 Message-ID: To: Rasmus Schultz Cc: PHP Internals List , Rowan Collins , Marco Pivetta Content-Type: multipart/alternative; boundary=94eb2c06d756965ab305375fda9f Subject: Re: [PHP-DEV] Cascade Operator (was Re: [PHP-DEV] Proposal for php7 class method return) From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --94eb2c06d756965ab305375fda9f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 11 lip 2016 18:31 "Rasmus Schultz" napisa=C5=82(a): > > > what's the actual added value of this sort of operator? > > The only direct value to the language is brevity - mere convenience. > > But I think the biggest indirectly added value, is that this will > discourage people from the chainable methods anti-pattern - so this > has value to the community; those who write chainable methods can > stop, and those who want chainable methods can get a wider choice of > libraries that were previously too verbose in use for their taste. > > > For this sort of chaining I'd propose, in addition to the cascade operator, > > the chain operator. This would work like the cascade operator except that > > the return of the function is implicitly the first parameter of the nex= t > > argument in the chain > > I don't like this idea, at all - I think we should aim for something > consistent with other languages. Per the Dart spec: > > > A cascaded method invocation expression of the form e..suffix is equivalent to the expression (t){t.suffix; return t;}(e). > > In other words, the expression always evaluates as the object, which > is fine for most cases, e.g. for everything you're doing with > chainable methods today - which can't actually return any values, > since they return $this. Note that something like PSR-7's HTTP > immutable models are actually factory methods, e.g. use-cases not > applicable to the cascade operator. > > The other marginal use case perhaps is cases where a builder of some > sort ends with a call to a factory method - this can be done without > adding a second operator, by using parens in a similar fashion to Dart > and others, e.g.: > > $object =3D ($builder > ->>setFoo(1) > ->>setBar(2) > ->>setBaz(3) > )->build(); > > Or obviously: > > $builder > ->>setFoo(1) > ->>setBar(2) > ->>setBaz(3); > > $object =3D $builder->build(); > > Regarding syntax - I feel the natural choice, e.g. similar to "." vs > "..", would be a longer arrow --> but that's ambiguous. (decrement > operator, greater than operator) > > The proposed |> operator looks horrible and is very awkward to type, > at least on both American and Danish keyboard - I use both. (it reads > like "or greater than" in my mind, so much so that glancing over > Sara's proposal earlier, I didn't even understand what it was.) > > Would something like ->> be ambiguous as well? That's fairly close too > - a double-headed arrow, not unlike the double dots of other > languages... > IMHO double-headed arrow looks pretty nice and whole idea is great. Very big +1 for this feature > > On Mon, Jul 11, 2016 at 5:33 PM, Marco Pivetta wrote= : > > Still interested: what's the actual added value of this sort of operator? > > > > Marco Pivetta > > > > http://twitter.com/Ocramius > > > > http://ocramius.github.com/ > > > > On 11 July 2016 at 17:28, Rowan Collins wrote= : > > > >> On 11/07/2016 16:15, Michael Morris wrote: > >> > >>> But for that to currently work setSomething must return $this. A cascade > >>> operator would avoid this. What about =3D> ? > >>> > >>> $a->setSomething('here')=3D>setSomethingElse('there'); > >>> > >> > >> This will be ambiguous with array syntax, e.g. > >> > >> $foo =3D [ $a->foo() =3D> bar() ] > >> > >> could be a call to $a->foo() for the key and to bar() for the value, o= r > >> just a chain ending with the array value $a->bar(). > >> > >> Naming things is hard. Inventing operators is harder. ;) > >> > >> Also note that it's the *first* call that needs a special operator, in > >> order to discard the normal return and substitute the left-hand side, > >> otherwise mixed chains become ambiguous and probably horrible to implement > >> in the engine: > >> > >> $foo->bar()->baz()=3D>bob() // what is $this inside bob() ? > >> > >> > >> > >> For this sort of chaining I'd propose, in addition to the cascade operator, > >>> the chain operator. This would work like the cascade operator except that > >>> the return of the function is implicitly the first parameter of the next > >>> argument in the chain. Something like this? > >>> > >>> $a->add(1, 2):>subtract(3):>add(4); // return 4. > >>> > >> > >> This exact feature was proposed a couple of months ago by Sara Golemon= . > >> See https://wiki.php.net/rfc/pipe-operator and the accompanying > >> discussion http://marc.info/?t=3D146196088900001&r=3D4&w=3D2 > >> > >> As far as I know, it's not been formally withdrawn, so might be > >> resurrected in some for for 7.2. It might be interesting to see a cascade > >> operator at the same time, but some of the same concerns that came up in > >> that discussion will probably apply to any proposal. > >> > >> Regards, > >> -- > >> Rowan Collins > >> [IMSoP] > >> > >> -- > >> PHP Internals - PHP Runtime Development Mailing List > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > --94eb2c06d756965ab305375fda9f--