Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105334 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43579 invoked from network); 23 Apr 2019 01:08:27 -0000 Received: from unknown (HELO mail-40130.protonmail.ch) (185.70.40.130) by pb1.pair.com with SMTP; 23 Apr 2019 01:08:27 -0000 Date: Mon, 22 Apr 2019 22:08:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=default; t=1555970921; bh=phzXH2cAjENpzdxa2It0gy/0wO/9FI1ziD8fun4DMDY=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=s8L0Owv2nzRMzvkPItEVsnVixzjXXXtO8KEbjhOG8nzbyzTciZ37w5GVm0k+7Ws9B usQZ+zenLnwrnHUSW1ga4vXPBtGh+IlmDCQ0WzdzLOrzCijITVgsJJwARamvaSB0gs m04cuaQ0FIq3X/uhYGuhVeIdUqzz5eOYajiC7KbY= To: Larry Garfield Cc: "internals@lists.php.net" 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=-1.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM autolearn=ham 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, Internals. I have been using HackLang for quite a while now and i believe they have a = better solution for this, and it would awesome to see it in PHP, the `as` o= perator. see : https://docs.hhvm.com/hack/expressions-and-operators/as --- when i see : ``` $foo =3D (Foo) $object; ``` i think that `$object` is going to be converted to an instance of `Foo`, no= t ensure that its an instance of `Foo`. Hack `as` operator ``` $foo =3D $object as Foo; // or $object as Foo; // use `$object` now, known that its an instance of `Foo` ``` > a `TypeAssertionException` expect would be throw if `$object` is not `Foo= `. 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 Monday, April 22, 2019 11:03 PM, Larry Garfield = wrote: > On Mon, Apr 22, 2019, at 4:47 PM, Benjamin Morel 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 > > Hi Ben. > > First thought: I'm all for easy ways to be more type-explicit, so yay on = the concept. > > Second thought: That said, how many use cases for that are there other th= an function boundaries, which we already have covered? > > I can think of two: foreach() loops and returns where you know the return= type with more specificity than the method you're calling. Example: > > /** @var Foo $foo *// > foreach ($arrayOfFoo as $foo) { > $foo->bar(); > } > > Example from PSR-14, in which you know the object you're getting back MUS= T be the same one that's passed in but dispatch() has no return type: > > /** @var Foo $event **/ > $event =3D $dispatcher->dispatch(new Foo()); > > The second I can see being easily handled by this syntax. The former, how= would that look? > > Third thought: Casting is the wrong name here, and feels also misleading = as a syntax. (float)$anInt means "type coerce this thing into a float", whi= ch cannot error. You're suggesting (Foo)$bar to mean "if this isn't already= a Foo, throw." That's a very different behavior semantic for the same synt= ax. Is that a land mine? I would expect (Foo)$bar to mean "recast $bar into= an instance of Foo if possible, and error if not". Which... I suppose "is = it already" is a subcase of that, but it's still not the behavior I'd expec= t from that syntax. > > --Larry Garfield > > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------------------------------------------- > > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php