Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108064 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81406 invoked from network); 9 Jan 2020 17:00:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jan 2020 17:00:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 78B37180504 for ; Thu, 9 Jan 2020 07:06:11 -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-io1-f41.google.com (mail-io1-f41.google.com [209.85.166.41]) (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 ; Thu, 9 Jan 2020 07:06:10 -0800 (PST) Received: by mail-io1-f41.google.com with SMTP id i11so7377572ioi.12 for ; Thu, 09 Jan 2020 07:06:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=aMKS1KLcm1qOnhWKXbYm1kav4Tp8h1tA+qRqorwGYsQ=; b=ADvvIBE7kWsD7y3lC8cqQrEgjLb1Zfl01f7MIlt+0liZucmEiE7HTZ19Ihnb2SFG2Y fm2ADOGeJa0kiFiGNIhWcrSb1bh0FT2KLWR5RHc7CHJpi4MFRYFj3EOS72l09OFxxoXK SMy4rNyYmaNULaC4VgXXTbDEHwkCzo58Ql+qO+caU20zGopzLmwaGhsrKv2iVv3YYt+b qnLusphqnYS/Cq/ZJeRyp19NXsacJYh/P6/E5z8Z5t/y1LjsXXcwkQo6/MnGiSjIGyaD O2ehGamtvXSwtu3D25gHMx4BS0saby+v6HZotr0aw+/mYTuKr+5bQ2C+/zfrsTWZ0dgq x/AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=aMKS1KLcm1qOnhWKXbYm1kav4Tp8h1tA+qRqorwGYsQ=; b=MWvesKIUnVC8wt1GqM3vBh0gMBougoKImeSz/6dBSMMRc9f6ONafHTJ/wjz1x6GwCl AqsYR+thnCc5ChPFc5mzXDKQLhoc26kkMEFPqc2mPqxf49F2zqhYtllT09BNw7/8IiVq XdC2dQLT1ZpkNNG4x+kIqPHAXmmlwcULdYRIUPhC0DldSdKDunTYZ7N2zzAGnvcS9C6V 8qSit3sl+Ta4serxAl+R4+HNWAdCqavuXf0fMCjN7G4xtaSDhAPUuqqNhSHoOdx1gLyL yagtKtRyCPLaDpIs3hY4839QrJcHy247igKvVURID8LGisYmNB7qgUC11a38gUfX9QQn cxZQ== X-Gm-Message-State: APjAAAV6WYEEcqFgehB4lXIxj9XICDJXHQTC+99GA4g5L5RsEUKeqWBj tLzqo5EOVJGik1Ix39K4w29LTWBXuY0X+0p7d8jBKw== X-Google-Smtp-Source: APXvYqwexklg2zL3M3sTiD8R09XwrYjlKSqB2pQZlDVZeFg5hut0IjBPEEGzUgLXPZjCamdUbz3xOKr2pFiS6jgXiv0= X-Received: by 2002:a5e:8612:: with SMTP id z18mr8346478ioj.206.1578582370068; Thu, 09 Jan 2020 07:06:10 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 9 Jan 2020 15:05:58 +0000 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="0000000000006aac00059bb65945" Subject: Re: [PHP-DEV] [RFC] Static return type From: rowan.collins@gmail.com (Rowan Tommins) --0000000000006aac00059bb65945 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Jan 2020 at 14:23, Andreas Hennings wrote: > > However, $this is not a real type, and it is unclear what the advantage > of specifying $this rather than static would be from a type system level > perspective. > > Perhaps not from a "type system level", but from a more broad "enforced > contract" level. > E.g. IDEs or code inspection tools can warn if a method will not return > $this. > This also means recursive calls also need to return $this instead of > static. > > class C { > function foo(): static { > return clone $this; > } > function bar(): $this { > return $this->foo(); // IDE can complain. > } > } > I think there are two different purposes for annotating return types: 1) Contracts that tell the caller what values they should expect when calling the function. 2) Constraints on the implementation which don't affect the caller, but allow the author to catch certain bugs. The primary purpose, in my mind, is (1), with (2) generally coming as a side-effect: if your contract is to return an int, then tools can warn you when you don't. All concrete types can be seen this way: scalars, classes, and pseudo-types like "iterable" are all contracts that calling code can rely on. "self", "parent", and "static" fit into that list fine, because they're ultimately specifying a class name. The special "void" keyword doesn't actually make sense as a contract given PHP's calling convention - as far as the calling code's concerned, it's equivalent to "null". So it exists only for purpose (2), constraining the implementation for the benefit of the function's author. $this would fit into the same category - as far as calling code is concerned it is equivalent to "static", unless they're doing something very specific with object identity. This makes it an odd constraint on interfaces, for example - DateTimeInterface exists specifically to have mutable and immutable implementations, and a return type of $this would explicitly prevent that. If we add ": $this" alongside ": void", I wonder where that takes us next - what other constraints on a function can be expressed using that notation which aren't contracts on the value seen by the caller? If we don't want to explore that question, should we avoid adding ": $this"? Regards, -- Rowan Tommins [IMSoP] --0000000000006aac00059bb65945--