Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108068 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5297 invoked from network); 9 Jan 2020 18:46:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jan 2020 18:46:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AB13B1804F2 for ; Thu, 9 Jan 2020 08:52:25 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (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 08:52:24 -0800 (PST) Received: by mail-qt1-f173.google.com with SMTP id j5so6388110qtq.9 for ; Thu, 09 Jan 2020 08:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=U+Tc83u9UNYvkRFkLS5f7rwuL9BYroBFwu0gf5V5TVU=; b=lnP4Fv3HI3i/9uNy4PX+0GUWAOXB0VNrlcuCqP0MlDXOtcHmsMYq5MMC6RbDPsihaZ rbN9q8dUpXv4JGCh06Q+9Tb9ITpleJX7VuRMLvIJ90Uesc7LU/M/6Hz9NcUMeURnUWr9 UycWnDZaMSgRQ0INJqdhNfgFChiRgSTgNY4nHh54bSG26k8qn1FtuMenecuioZy/9WGM q9cZpg8TFERJYaFch3aCBukZbng91OQHaFyDvOeWjvByOZFY7Tfj8wvmmqgehl6K5Ytt purUW6cscTJFMefLteKJ9H5tA/JKJts/Kg3ErH60hz9JCInu0b4qHhnyQ1UUh0j2waPl IDvA== 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:cc; bh=U+Tc83u9UNYvkRFkLS5f7rwuL9BYroBFwu0gf5V5TVU=; b=XtnBrqPUYPSUrXnJorl1YSO5IVT9dkAuTPkfi/AlNxh0EAZqHh9Sqhpsv3wA1jjf7j nY0s2cFhYzqrJgsoDX1hF/ZqW/279I5yGoGcxPtm3ZYKGdRrVL4A+g5bya7r7bJhIJvA Es1X9NRkexcFXTL2x8xhWTq+TFufqHR/x7sZigwfEDEVebkBeczkIhQqsaypIDu+fRMy qTQ7dXaoXMLEVP6yInzY2uIhyfKw5rhZnw1dIEUE5hOjTfP2tpNlXF9cKU3k3AXYxsGC Q+oD3gQrI3aL5Jthb1ddUqCUvWSXm/VIcZMWqC+wtHkPfjH6MHhiyUWAWz76awYwtCQ8 VoqQ== X-Gm-Message-State: APjAAAX6+wdlWkUa6eZ4oJbNj7Z1xEwP/m5QygQKOC12J9XdI+pYaD7w SUuXYkUG9QlBVrOoJPLtY7j8iIjXaAE= X-Google-Smtp-Source: APXvYqyLPvhRHJPfABFi7dcJUDTVcDjJ+VTOZG9hgg5Q21DM4dDjry/Ef8yLw/6+1YqbQMRv0dvEaQ== X-Received: by 2002:aed:25d1:: with SMTP id y17mr8900475qtc.41.1578588742398; Thu, 09 Jan 2020 08:52:22 -0800 (PST) Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com. [209.85.222.174]) by smtp.googlemail.com with ESMTPSA id 68sm3401151qkj.102.2020.01.09.08.52.21 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Jan 2020 08:52:21 -0800 (PST) Received: by mail-qk1-f174.google.com with SMTP id w127so6567015qkb.11 for ; Thu, 09 Jan 2020 08:52:21 -0800 (PST) X-Received: by 2002:a05:620a:1310:: with SMTP id o16mr10137658qkj.383.1578588740816; Thu, 09 Jan 2020 08:52:20 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 9 Jan 2020 17:52:09 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Nikita Popov Cc: Rowan Tommins , php internals Content-Type: multipart/alternative; boundary="00000000000024958a059bb7d5a9" Subject: Re: [PHP-DEV] [RFC] Static return type From: andreas@dqxtech.net (Andreas Hennings) --00000000000024958a059bb7d5a9 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Jan 2020 at 16:31, Nikita Popov wrote: > On Thu, Jan 9, 2020 at 4:06 PM Rowan Tommins > wrote: > > > 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"? > > > > An argument could be made that $this does also specify a certain contract > to the caller: That the API may be used fluently or not without change in > functionality. That is > > $foo->setBar(); > $foo->setBaz(); > > must be strictly equivalent to > > $foo > ->setBar() > ->setBaz() > ; > > The same is not the case for a plain "static" type. In fact, I think that > for all other uses of "static" outside of fluent interfaces, not using the > return value would be a programming error. > > But still, this kind of contract is not a type-system contract, and I'm not > sure it's a good idea to mix other types of API contracts into the type > system in this fashion. > We currently don't have another "system" where this kind of info could be specified. And in this case, $this also implies static, so if we would use whichever other system to specify $this, the type hint "static" would become redundant. "@return $this" is common in phpdoc (*) since a while, and so far I have not seen any problems with it. If we ask "where does this lead?", one natural next step might be to specifically say that the return type is the same type, but NOT $this. So, a promise that we really get a new object which we can modify without changing the original object. I don't know how we would express this. Perhaps ": clone", but this would be too specific imo, because whether it is a clone is an implementation detail. In phpdoc (*) there is no common syntax for "static, but not $this", so perhaps it was not needed yet. And in fact, an immutable wither can safely return the original object if no changes are needed, if other methods are also immutable. (*) When I say "phpdoc", I really mean the kinds of docs that people commonly use in the wild, and that are understood by IDEs. Which of that is documented and supported by the phpdoc library is a different question. > > Regards, > Nikita > --00000000000024958a059bb7d5a9--