Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105351 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31878 invoked from network); 23 Apr 2019 12:33:26 -0000 Received: from unknown (HELO mail-40136.protonmail.ch) (185.70.40.136) by pb1.pair.com with SMTP; 23 Apr 2019 12:33:26 -0000 Date: Tue, 23 Apr 2019 09:33:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=default; t=1556012026; bh=AoyB6Unb39rx9uK8x0eCvrioLD/ja3jxm6PcC2wHRww=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=TQV2oI6ZlKf2xUMcxLDPiJDF4EpnE3mYZemX+Vtzj6WU19+uHETNAcs1BjJlVZAPd UorfSAOEWMejMOiZpok3F9KB2y6FslJW+ImBa/azhNeU1YxNbEV1ViA86vfA3x7G6M y2c2OiPkSniCAkRFfx80eVV7gJzpu5JEloR6+WKM= To: Nikita Popov Cc: Benjamin Morel , PHP Internals Reply-To: azjezz Message-ID: In-Reply-To: References: Feedback-ID: 2QbNVvXqZX7F9J8FTDAv0u3ryua4T0_lfklxZ8hRMzvBUxf8m8U50Gi9tVWG9V2LePU31MFOrVnxPKYLJjZPXw==:Ext:ProtonMail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FREEMAIL_REPLYTO autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: azjezz@protonmail.com (azjezz) Hello Nikita, I would love to hear your opinion on the `as` syntax from Hack, and whether= it can be used in PHP the same way or would it be an issue. Cheers, - Saif Sent with ProtonMail Secure Email. =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original Me= ssage =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 On Tuesday, April 23, 2019 10:16 AM, Nikita Popov wr= ote: > On Mon, Apr 22, 2019 at 11:48 PM Benjamin Morel benjamin.morel@gmail.com > wrote: > > > Hi internals, > > I'd like to revive an old discussion https://externals.io/message/67131 > > about > > object type casting. > > The idea would be to allow (ClassName) casting: > > > > $service =3D (EmailService) $diContainer->get('email.service'); > > > > > > The above code would throw a TypeError if the value is not an instance = of > > the given class. I see the following advantages: > > > > - Type safety: we can be sure that the value is of the correct type o= r that > > we'll get an Error. This syntax allows to fail early if the variabl= e > > happens to not be of the expected type, and avoids much more verbos= e > > checks; > > > > - Static analysis: IDEs and static code analysis tools can now unders= tand > > the type of the variable, without having to resort to `@var` annota= tions. > > > > > > These combine into a third advantage: readability. Today's equivalent o= f > > the above one-liner could be: > > > > /** @var EmailService $service */ > > $service =3D $diContainer->get('email.service'); > > if (! $service instanceof EmailService) { > > throw new TypeError('Expected instance of EmailService, ...'); > > } > > > > > > Which is a lot of boilerplate code that could be easily avoided by > > introducing this new syntax. > > Before moving forward and working on a formal RFC, I'd like to hear you= r > > thoughts: what's your early feeling about this? Did I miss other > > discussions around this subject? Are there any technical issues that co= me > > to mind? Could this feature help the upcoming JIT compiler produce more > > efficient machine code by knowing the type of the variable at compile t= ime? > > etc. > > Note: "casting" might not be the perfect name here as what we're really > > doing is a type check, but this reuses the type casting syntax and > > resembles Java's object casting. > > Thank you, > > Ben > > Without commenting on the rest of the proposal: It's not possible to use > (ClassName) as a cast syntax, because it is ambiguous. For example (Foo) > [$x] is already valid syntax (fetch constant Foo and take index $x), or > (Foo) +$bar, etc. > > The only reason why (int) etc are okay is because we treat the whole (int= ) > as a single token, something we can't do in general (because it would bre= ak > foo(Foo)). > > Nikita