Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94464 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55389 invoked from network); 11 Jul 2016 12:18:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2016 12:18:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.47 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.47 mail-wm0-f47.google.com Received: from [74.125.82.47] ([74.125.82.47:37174] helo=mail-wm0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D5/B6-22463-09E83875 for ; Mon, 11 Jul 2016 08:18:25 -0400 Received: by mail-wm0-f47.google.com with SMTP id i5so13346412wmg.0 for ; Mon, 11 Jul 2016 05:18:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=FaOWc1A9+5XNejA3nFEf2YQZXhuaN2rH7o9LjQYldQM=; b=nCPBujxkavDC+tGvzaBdbhZy8SsZeHSH9e5sSrAXVMEVX+k2KOTY2CdZaZ1LHdNGMS /svidQTghV1MpGrRI0h6RCN6vkokHeUJ8g4VNie/ReTVp7QpXYk7xQqXwo0Ou9DihNIC QSH4klz4jcVqa9WNJNieuELmHA7P40/Ik2eNlDwtPNcwWckEZgQoUC2t+T82gbHzSgRB EWUyOetahr+LMUeZpgqDOSAxdzEfXI8Mk3RtUaX8lt+1DJsru/Z3QTAgByfxnqL/Anvp vLKrXMbjDbFDnQdsm1CkFtSCQO6Kk7DUVuVYvdE6aLHAH7MDgsxbcT7F3YmNHDwOwikP mXbQ== 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=FaOWc1A9+5XNejA3nFEf2YQZXhuaN2rH7o9LjQYldQM=; b=I58E5huEtGX4yt8gFVUt3W/6e/g9Atz20azlz+SWvWjP1/ttXzNvMnLF9ecopgZq1q oZHLGbMWYCOPPsyM2lhw4qL+6z0ntqj7HHPZxMTiNUonR67+K2+jksmeyHtjGDCIBdGI xuNtzXyaTBjhEh5vvU0+CkaMSsaspPMI5fdrJTgvsmYF/k1+wFjZTx+08QdFl+ZCsfYE Qxj/R15U+wjCs2W4dNEuASR9s9/L9DqiOI5ToMRAaQmOdaiAa9J5p/83sPcLsYtXpN/T WOu1lpGbVs/4EJBLbydrbKiYBS7MkFY6bI3B94+3s7FGAYP/NtnoWFIlWuHme+8Gv6zt MY7Q== X-Gm-Message-State: ALyK8tJi5GkPeeIuV3x5NDGBF61CPlIhCh7lCgwKmCmZ+ODMX85SH+bAYiv77+Tw1EAPsacrVTG5ASfgBZVQHg== X-Received: by 10.28.126.195 with SMTP id z186mr14553251wmc.95.1468239502039; Mon, 11 Jul 2016 05:18:22 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.37.202 with HTTP; Mon, 11 Jul 2016 05:18:02 -0700 (PDT) In-Reply-To: References: Date: Mon, 11 Jul 2016 14:18:02 +0200 Message-ID: To: Jesse Schalken Cc: Rasmus Schultz , Daniel Ciochiu , PHP internals Content-Type: multipart/alternative; boundary=001a114174e0f6e13505375b25a5 Subject: Re: [PHP-DEV] Proposal for php7 class method return From: ocramius@gmail.com (Marco Pivetta) --001a114174e0f6e13505375b25a5 Content-Type: text/plain; charset=UTF-8 Hi Jesse, On 10 July 2016 at 14:31, Jesse Schalken wrote: > On Sun, Jul 10, 2016 at 8:34 PM, Rasmus Schultz > wrote: > > > > > What you're really trying to accomplish is something like the ".." > > operator found in some other languages - this is known as the "cascade > > operator" in Dart, for example: > > > > > > > http://news.dartlang.org/2012/02/method-cascades-in-dart-posted-by-gilad.html > > > > > > While I was writing about an operator to set object properties in-line > here: http://news.php.net/php.internals/93662 , I considered it would be > easy to add method calls to the same syntax: > > > return Blah::create(5) { > prop1 = 'hello', > prop2 = a_func(), > setThing(9), > setOtherThing(1), > }; > > > which is in principle just an alternative syntax to as the cascade operator > in Dart. > > I agree entirely that method chaining is just a verbose workaround for the > lack of such a language feature. I've found a few problems with method > chaining which this would resolve: > > 1. The verbosity of adding "return $this;" to all your mutator methods > (Daniel Ciochiu's issue). > `return $this;` is a fairly common anti-pattern. It can be useful in few edge-cases, like immutable APIs (CoW) and DSL builders, but it's usually a bad idea. I ranted extensively about this at https://ocramius.github.io/blog/fluent-interfaces-are-evil/ > 2. You can't return a value from a mutator method while also supporting > method chaining. In theory mutator methods shouldn't need a return value > (command-query separation) but in many cases they do, such as a setter > which returns the previous value, a pop() method on a stack, or a > delete($where) method returning the number of deleted records. > 3. It is often unclear whether a method being chained is returning the > same object it was called on, a copy of the object, or an instance of a > different class altogether. You can work around this with careful method > naming and documentation ("with.." for new instance, "set.." for same > instance etc), but language-level support eliminates this issue > entirely. > Are you actually talking about a `$this` return-hint? That would indeed solve some issues, by enforcing identity as part of the return type. Don't think that there are scenarios where identity is required though, as it would be useless (the hint would not serve any actual behavioral/expressiveness improvement). @Daniel: the fact that an implicit return is added to the opcodes for a method that has a `self` hint seems useless/magic to me. It would basically allow automatic fluent interfaces, enforcing something useless though, since a fluent interface does not provide any actual usefulness per-se (besides code-style preferences). Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --001a114174e0f6e13505375b25a5--