Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119209 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71652 invoked from network); 25 Dec 2022 05:53:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Dec 2022 05:53:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6D49A180089 for ; Sat, 24 Dec 2022 21:53:14 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,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-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 ; Sat, 24 Dec 2022 21:53:13 -0800 (PST) Received: by mail-pj1-f47.google.com with SMTP id o2so2779841pjh.4 for ; Sat, 24 Dec 2022 21:53:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ErvpCEPXHuyvcwlkOJpXICrYfXeAe4GuUL6UTc4qvYc=; b=dk7FQw09xc1ge0v/JfgBUbWp4uikL4DfOje5oOyaqtXRYBQ0TTDqM66ygKURQYj0wC GdInq8VB0y07L5t6umYXm1Ip8Rv1g+biIX+2TSM1XJydMPewjcgfI05aFaiHANbNsvHY CUjA31s2dqy1JI11EiZWIxC8bjqxFLnQRJzQjIVmZZVAJUH5Rvzc7rAodfR1qPA0Ln+P /Y4GfSZujXCALIFWd1qG68hAHoizwl5mv6Rv1takFZ2+vzNb+GUI9Y2khOs8tufnmaub e/1ZzuQWBRLChWr/rtX+9Wzes8tklvz608DNn/bDyHhD7QfzfEyfx9PusfSXua8sJcmY mgiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ErvpCEPXHuyvcwlkOJpXICrYfXeAe4GuUL6UTc4qvYc=; b=A3TFQWhBrwpZ/OeAXuDvxre102pEz/MBYGoPXwsJF50brQCM5PlzUtcuCs+nzDfRNv 0MgN0LbK3bOKtEv8VjbsyawD2PH7NH6KeCYHFw1stvr5UDhaKOBkmgc+0xpoR38f8uU+ zp1qdwIQFmSkIbvs4j8XT7AeGJsCEg8TZ3F6ZyVZ9iRF7JM2apddXQmgQfuyvpPVC4PR eGXgIY6AFE+oc2O6l/bKHQ2XHRUrKU8Gj1DFuWqeVsRFqORShxOSSNCQhwtV3Ko1NNM2 eLBwlp9S5HwgPE0GvyajdqKcnSLelK4JFXgXNw84duDM64ptsREVHEnWcJ7TynofES5L +P1A== X-Gm-Message-State: AFqh2kpmXzGlIlKeUBucjJXYiPmmJx25TZJOQJ1nHnIwj5WHP9Iokq0D 4J5sMGqPPTWQDUBX4d6QAy3OM1fESwfkuj915I6SR7rcZZU= X-Google-Smtp-Source: AMrXdXs0fODYuKeLSwN9tXMY2QW7HNDrVlv8X8RMbfiVypJO8I6Gx+3iyeNACQZJW+LZM5T9zSpSDYVJFHePz1IcHeo= X-Received: by 2002:a17:90a:d350:b0:21a:c45:a8b0 with SMTP id i16-20020a17090ad35000b0021a0c45a8b0mr1229224pjx.205.1671947592791; Sat, 24 Dec 2022 21:53:12 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 25 Dec 2022 06:53:01 +0100 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Methods which auto-return the class instance From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Daniele On Fri, Dec 23, 2022 at 1:09 AM joke2k wrote: > > Hi folks, > > What do you think about having a method which returns the class instance > `$this` by default only IF its return type is set as `self`? > > It is very common for fluent class methods to have a verbose `return > $this;` ending in their body. Instead of adding special syntax to make writing fluent accessors easier, I wonder if something akin to Dart's cascade operator would be a better approach. https://dart.dev/guides/language/language-tour#cascade-notation $foo ..setBar('bar') ..setBaz('baz'); The syntax is just copied from Dart, there's probably a better fit for PHP. .. would invoke a method call but discard the return value, evaluating to the lhs of the method call instead. With an operator solution one can use fluent method calls on APIs that aren't specifically designed for it when desired, accessors can become leaner as return $this is no longer necessary, and it becomes obvious at call-site that the chained methods are called on the same object. I'm not sure about syntax or the fact that Dart allows property assignments in combination with .. as that makes the syntax ambiguous (or whitespace sensitive), but that wouldn't be necessary for what you're proposing. Example of the ambiguity: $foo ..bar = $bar ..baz() // Could be parsed as $foo..(bar = $bar)..baz() $foo..bar = ($bar..baz()) An alternative might be a C# style syntax which would make property assignments unambiguous. $foo { setBar('bar'), baz = 'baz', } But this is also longer and less readable (to me at least). Anyway, just my unrefined thoughts. I would not support implicit `return $this` for self or static. At the very least, the return type should be $this (which is essentially a sub-type of static) but that too doesn't seem like an elegant solution to me. Ilija