Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116897 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66558 invoked from network); 19 Jan 2022 13:56:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Jan 2022 13:56:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AB483180539 for ; Wed, 19 Jan 2022 07:07:33 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 19 Jan 2022 07:07:33 -0800 (PST) Received: by mail-ua1-f50.google.com with SMTP id m15so5012803uap.6 for ; Wed, 19 Jan 2022 07:07:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PrLm/2Ac5LVcLZWHqvzZNs+zd7st5P0Jgzh35qvGh4A=; b=pEeRxxsUM++9K3bS/lPjhkPVAfGO/AJmoMlCroC9gU60Ivcsw8td20TMaHHvAIyZGm q+4YPBMU8JIdlSs3lH/vIcmlwQC/fO/jIcM8P8isS31Ce5K0mjJsxBRhzxCFWCv5YZph r6xwNRDLSQfrMcPeNsM/oC8CaOQ5hS0oifG+wTLf1E9yr4sIXe2dNe7EvOizij/8+Izc YaRPtqWnUWqEw2IDsbjaPuwbAn/Xz1BuH+uvYzzV523kTwysdSUlRG4J239ZkPo6jza3 KHUSg95z9zZ2czIduLc3+YB491cLQ8H+MBmocrdkYdazTkPAiMl0p1TxMAcaQFqa6exZ Lu8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PrLm/2Ac5LVcLZWHqvzZNs+zd7st5P0Jgzh35qvGh4A=; b=7pTGAzsDeUITLwd8iSvbSlybmj6IRg6sACND4OzAPGEDHOKa+C24RmcxPY9CfOPrCK XBr29nPeQNiU5UYh2hsdhVX4xeXEy1Z5emoUhD86tdJULoInYiRie21CIfzYGwN2/Prv 5Z3LELGphmxcX83GZhuutn86AtGNDgLgYkfmhBkA8PEe+LbVJpyCun9ayJuWDgzPw/j0 gXuIfWQdl4l/8jAsW/ovJhAKw7MzIj4UyaGZyFZ21OAe0Z4HTGvHnHG++A+XDeK6uRNf t0Pmvgx+vKSZmNQR3nMfnR6uA3fbrTrDMbcYq3iOCMOWkzMaGMrGFwaIvqIDHfR9oqyA T8aw== X-Gm-Message-State: AOAM533agX6G8AzzxiyYKawSZx9eCAQgGHWFA1clCmJtLDGoKKbp8rgU ayJ26ac8uEX4nxm3JZnfkynLBkgW3C7YcB0MBvg= X-Google-Smtp-Source: ABdhPJyIzYt5crCSqkrhus7W4RQ/1Iwrxh/iU79q0D3zHetIG9idXDmAmKbgfM5CtELs9RvnCEx7xtVTz7bL4k516gg= X-Received: by 2002:a67:ba17:: with SMTP id l23mr12171333vsn.33.1642604852559; Wed, 19 Jan 2022 07:07:32 -0800 (PST) MIME-Version: 1.0 References: <1641335738.195767637@f174.i.mail.ru> <5a4aebf8-e592-4517-8930-d18b112ef1fd@www.fastmail.com> <5a99809d-afda-546c-5a11-a4f0f821aa37@korulczyk.pl> <6E50C348-9452-40E1-ACE8-06DED11C9136@gmail.com> <9070564d-e138-81f6-c757-812d8962416e@korulczyk.pl> <23f78a61-a73e-eaa3-861f-75e9bca28485@korulczyk.pl> <262352f2-a3e6-272d-1118-5455497217a7@gmail.com> <6ABDE9F5-C69A-4FEE-9736-F2DED514184A@newclarity.net> <3543d7e3-9a26-25f8-8c48-d646431e84a2@gmail.com> In-Reply-To: Date: Wed, 19 Jan 2022 10:07:20 -0500 Message-ID: To: Jordan LeDoux Cc: Rowan Tommins , PHP internals Content-Type: multipart/alternative; boundary="000000000000be441405d5f0be03" Subject: Re: [PHP-DEV] RFC: Trait expects interface From: chasepeeler@gmail.com (Chase Peeler) --000000000000be441405d5f0be03 Content-Type: text/plain; charset="UTF-8" On Tue, Jan 18, 2022 at 3:55 PM Jordan LeDoux wrote: > On Tue, Jan 18, 2022 at 11:13 AM Rowan Tommins > wrote: > > > > > The difference with the "trait requires interface" proposal is that I > > don't understand what advantage it gives the author of the trait. What > > decisions can you make as a library developer because you've said "users > > of this trait must implement the matching interface" as opposed to "... > > can implement the matching interface"? > > > > It's possible there is some advantage I'm missing, but so far nobody > > seems to have presented it. > > > > > Well, the trait doesn't necessarily have to fulfill the entire interface, > first of all. As you mentioned, this can be worked around using abstracts > in the trait. However, what if you're dealing with return values within the > trait? > > Suppose I have something like this: > > trait ArithmeticTrait { > public function add(float $val): NumberInterface { > // Do math > > return $this; > } > > public function addmul(float $val): NumberInterface { > $added = $this->add($val); > > if ($added->isPositive()) { > // Do stuff > } > > return $this; > } > } > This can still be handled with abstract methods trait ArithmeticTrait { public function add(float $val): NumberInterface { $n = $this->getNumberInterface(); // Do math return $n; } public function addmul(float $val): NumberInterface { $n = $this->getNumberInterface(); $added = $this->add($val); if ($added->isPositive()) { // Do stuff } return $n; } abstract protected function getNumberInterface(): NumberInterface; } class Foo { use ArithmeticTrait; protected NumberInterface $n; protected function getNumberInterface() : NumberInterface { return $this->n; } } > > In this situation, the return value of the trait requires that $this > implements the NumberInterface, however there is no way for the trait > itself to enforce this. > > Jordan > -- Chase Peeler chasepeeler@gmail.com --000000000000be441405d5f0be03--