Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106547 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12338 invoked from network); 11 Aug 2019 17:01:58 -0000 Received: from unknown (HELO mail-ot1-f49.google.com) (209.85.210.49) by pb1.pair.com with SMTP; 11 Aug 2019 17:01:58 -0000 Received: by mail-ot1-f49.google.com with SMTP id r21so144269163otq.6 for ; Sun, 11 Aug 2019 07:29:53 -0700 (PDT) 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 :cc; bh=5d5NOdm4ONN8uhsHNZuyHdhKuvSAxaIvUvPDAOx+GW8=; b=CO8CtF+bSkn32oo+n4tQsFQotjaOB51tRnmg/ttCR5wOOBZ4WBmSFnfA12V9t+yQMf 7I3I4WbSFgWXpyCNHeNg8QG5xjfEXGJOM6NG6kmGL1mFlToS6B3SqN8NXiWit5t8gC8e QWghQ33ieySRtBAtHyaC9OZGlX81bYlQLVs8EDo290TwFyl8DzxpzFxzsFKUgtZehS11 BImsWpqfuJQ+6MnsEC34PGAyTyytpl5oLoGiMdoyK6xYvov4UXvg9IDG86kFawjv5e43 aCglM58+oXpThIO99EiGLM2sNr+3JVFjvXf01kkOQv/+WqndiKmJw2j+WZIFYcRd3YmP mCfg== 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=5d5NOdm4ONN8uhsHNZuyHdhKuvSAxaIvUvPDAOx+GW8=; b=jox0UFPskepNwreuDFbBkCwM5BrLpLe57PSrp/F3Q95DT4GsGvp46N6t6nG35PDFKz rsnNLCYmY9Uqq6qqfstdK74TROSKYAQXU+yAyvZ8Frjt10cIp3TarZ8PKBtmfmy/9k1z UMyh7qT5Y1tssNU2bTtpAxJNGmk+PXqu/rOMeUGtwMWsrnaovDLSjB9BUSuGe/MIvAp4 GLRUlsYiCTfDh0k+GF1t/9azkhqN85oRMFgo0x5G1+vQA6mQ8qti2PV/u+YBsi9dTLXT MZKHV5PR8Agrk+XzXySBG2hw4Y3kjyLIotVnMc15Pa+KP6PjfdRPUzKWypgma9e/0GHo CU7Q== X-Gm-Message-State: APjAAAWutyHkLbC0GyWso5lHWicVQeFwJP4swPl0/JoyJzk3GwqriIka 0udMuVJO+s/5rc7DmVmiM6bxwdw5ZpMs5WkIO6o= X-Google-Smtp-Source: APXvYqzd9e8Goep7QoPECQuc6fTxEc9kH1TEDkCIC6tynxIhU9Ytnm6BCtXBGJvVy2JLONWWAjipMlzjdjH2jV+UC0s= X-Received: by 2002:a9d:ee9:: with SMTP id 96mr6085758otj.215.1565533792992; Sun, 11 Aug 2019 07:29:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 11 Aug 2019 16:29:41 +0200 Message-ID: To: Nikita Popov Cc: Sara Golemon , PHP internals Content-Type: multipart/alternative; boundary="0000000000009d833b058fd83d6a" Subject: Re: [PHP-DEV] Adding return types to internal methods (PHP 8) From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --0000000000009d833b058fd83d6a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Le dim. 11 ao=C3=BBt 2019 =C3=A0 15:24, Nikita Popov = a =C3=A9crit : > On Sun, Aug 11, 2019 at 3:01 PM Sara Golemon wrote: > > > On Sun, Aug 11, 2019 at 7:51 AM Nikita Popov > wrote: > > > >> On Sun, Aug 11, 2019 at 2:37 PM Sara Golemon wrote: > >> > P.S. - Perhaps a way to the middle might be to introduce implicit > return > >> > type hints. If a child method is implemented with no return type > >> specified > >> > on a parent method which has one, then the parent's type is assumed = at > >> bind > >> > time, then we provide a better runtime error if that assumption is > >> violated > >> > in a strict_types=3D1 context which is the only place where strict > >> covariance > >> > of return types matters to PHP. > >> > > >> > >> This is an interesting idea. There's still a BC break here, but it's > more > >> limited and only applies to code that is actively violating type > contracts > >> (that were previously implicit). Imho this should not be bound to > >> strict_types though: strict_types controls coercion behavior of scalar > >> type > >> annotations, and nothing else. It should not be overloaded with this > >> additional meaning (and I don't think the BC break is nowhere near lar= ge > >> enough to justify this overloading). > >> > >> Fair. Just trying to tread as light as possible, but there is a > > vanishing point. > > > > > >> Do you have this in mind as something for use by internal classes only= , > or > >> as a general language feature? I can see the use as a general language > >> feature (see for example the debacle where PHPUnit added "void" > >> annotations > >> to some methods). The migration problem of adding return types is > >> certainly > >> not limited to internal classes. On the other hand it does seem somewh= at > >> weird that an (inherited) return type will be enforced that was not > >> explicitly given in the source. > >> > >> I agree that it would be as useful to userspace classes as internal > > ones. It would allow library authors to embrace strong typing without > > breaking consumers who are already conforming to docblock types. > > > > It's probably worth proof-of-concepting this and making an RFC > > irrespective of the push to annotate all the things. If feasible and > > accepted, I'd be much more on board with including return types for > > internal methods in that plan however. > > > > After thinking about this a bit, this is going to be pretty hard to > implement. Currently return type checks are performed using a separate > opcode, which means that we need to know the return type at compile-time. > At the point where inheritance happens and we know the parent return type= , > we are no longer able to modify the opcodes of the function. We'd have to > integrate the return type check into the main return logic, with a possib= le > negative impact on performance... > Coincidentally, we're working on exactly this in SYmfony: we want to add return-type declarations in v5 (due in November) and we want to provide a continuous upgrade path so that apps that are using v4.4 are warned with a deprecation asking them to add the return type. We're doing this using `@return` annotation in the docblocks: when one is found in base Symfony class, child classes that don't declare their return type get the notice. Because we don't want Symfony to trigger the deprecation when extending itself, we silence it when a child class has an explicit `@return` annotation. This is the way to opt-out from the notice, while telling our userland what they should do, before we do. The related PR is here if you want to have a look: https://github.com/symfony/symfony/pull/30323 I don't know if you'll find applicable, but an idea for PHP itself could be that internal classes expose the planned return-type via a docblock, accessible by `ReflectionMethod::getDocComment()`. This way we could build userland tools to leverage it and trigger deprecations. In general on this thread, I agree with Nikita it would be desireable to add return types, and I also agree with Sara that it should not be a hard BC break. I hope this helped a bit :) NIcolas --0000000000009d833b058fd83d6a--