Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94470 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76357 invoked from network); 11 Jul 2016 16:30:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2016 16:30:11 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.213.46 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.213.46 mail-vk0-f46.google.com Received: from [209.85.213.46] ([209.85.213.46:35236] helo=mail-vk0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/1A-22463-E89C3875 for ; Mon, 11 Jul 2016 12:30:07 -0400 Received: by mail-vk0-f46.google.com with SMTP id v6so146081035vkb.2 for ; Mon, 11 Jul 2016 09:30:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=cNEqs22s01qFv8kb/C3wAuSTPH+rexmGPPkRJR/iRyI=; b=Zv8T03HOtsD2BQccIIz3htnOgmOddyPZRJY+qIsKxxJuEexVBQRO7Y9lysxbuyJ04b OF98adwQ+2h7UoHhO6D0XPwLTrC8KUUqqatU4GET9Xs7DeyhDuz2At9MmDdH8wzQo0a6 xjMqe/UC73Fyb2422zWTttdL/LMhYVvqztb0r0RBFYBOkFYtt1nZZsjHjG/gqTJt3RXT 0BsDAaSS8TDA96pbzXS6YTEXgNnaUTDbW+VvbkTSPgRiath/2ns4CR/aNEBT6fBno8l6 bRInqe3Bbf54+C6sB4ExRjFDaTBVfuba1eFx+AVbN5++KP3Xu+3HzTEHrjLTf81J9b2/ DP9g== 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:from:date :message-id:subject:to:cc; bh=cNEqs22s01qFv8kb/C3wAuSTPH+rexmGPPkRJR/iRyI=; b=Y2Fz7yCb2b9g0xySXWdhzw90rFS6lzo5adQ9oehKmBoktYckXzRJSUGtbWiEWFVMfW poQl5ak/MI9eJ1Y/9Rvj5Lg0qTIuKvMnKnlHe/fr6d4Ro97hi8xL8f/lvUUSodRl4OyW dgprRe25KjTZGKp1zTeZ4SDN1/W8oBSaHutQrn3edh+hrNi/KLuzZJoqoWQTmNpSuLX5 FEAJqsFp2/UG93aOcXIulBXqRngAX5QBNeYrZw8LKKGNP4gtmHGe0wLMiYDmCjO7C8Cm 3YnaIQo/bXiuvPWmtDByiUXyAAo4rRGrSgIhXkNopz+N9DoP932GIue2NUvy2jv88GE0 skHw== X-Gm-Message-State: ALyK8tIzcLc4lUNtuP1lTMUv53F4GdaIjlQsmBuFxr+8qM2yqZaCxDy2nIKmVSCwDPn8g9oYNNR/7yvukdMu1Q== X-Received: by 10.159.39.39 with SMTP id a36mr9603762uaa.86.1468254603385; Mon, 11 Jul 2016 09:30:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.105.9 with HTTP; Mon, 11 Jul 2016 09:30:02 -0700 (PDT) In-Reply-To: References: <86455a1e-eb57-1f30-9016-ac70c9f30bdd@gmail.com> Date: Mon, 11 Jul 2016 18:30:02 +0200 Message-ID: To: Marco Pivetta Cc: Rowan Collins , PHP Internals List Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Cascade Operator (was Re: [PHP-DEV] Proposal for php7 class method return) From: rasmus@mindplay.dk (Rasmus Schultz) > 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 next > 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 = ($builder ->>setFoo(1) ->>setBar(2) ->>setBaz(3) )->build(); Or obviously: $builder ->>setFoo(1) ->>setBar(2) ->>setBaz(3); $object = $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... 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 => ? >>> >>> $a->setSomething('here')=>setSomethingElse('there'); >>> >> >> This will be ambiguous with array syntax, e.g. >> >> $foo = [ $a->foo() => bar() ] >> >> could be a call to $a->foo() for the key and to bar() for the value, or >> 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()=>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=146196088900001&r=4&w=2 >> >> 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 >> >>