Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105350 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28746 invoked from network); 23 Apr 2019 12:26:27 -0000 Received: from unknown (HELO more.superhosting.bg) (91.196.125.214) by pb1.pair.com with SMTP; 23 Apr 2019 12:26:27 -0000 Received: from [94.156.78.222] (port=34538 helo=[192.168.20.137]) by more.superhosting.bg with esmtpa (Exim 4.91) (envelope-from ) id 1hIrhN-0000gf-76 for internals@lists.php.net; Tue, 23 Apr 2019 12:26:47 +0300 To: internals@lists.php.net References: Message-ID: Date: Tue, 23 Apr 2019 12:26:40 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - more.superhosting.bg X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - hristov.com X-Get-Message-Sender-Via: more.superhosting.bg: authenticated_id: php@hristov.com X-Authenticated-Sender: more.superhosting.bg: php@hristov.com X-Source: X-Source-Args: X-Source-Dir: Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: php@hristov.com (Andrey Hristov) Hi, maybe with short closures this will be pretty easy to implement by injecting a short closure that expects the return type to be of the type to be casted and to return it. Something in the like of : $service = (fn(EmailService $s):EmailService => $s ;)($diContainer->get('email.service')); Not that I support pulling services from a container most often being an anti-pattern. Cheers, Andrey On 23.04.19 г. 0:47 ч., Benjamin Morel wrote: > Hi internals, > > I'd like to revive an old discussion about > object type casting. > > The idea would be to allow (ClassName) casting: > > $service = (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 or that > we'll get an Error. This syntax allows to fail early if the variable > happens to not be of the expected type, and avoids much more verbose checks; > - Static analysis: IDEs and static code analysis tools can now understand > the type of the variable, without having to resort to `@var` annotations. > > These combine into a third advantage: readability. Today's equivalent of > the above one-liner could be: > > /** @var EmailService $service */ > $service = $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 your > thoughts: what's your early feeling about this? Did I miss other > discussions around this subject? Are there any technical issues that come > to mind? Could this feature help the upcoming JIT compiler produce more > efficient machine code by knowing the type of the variable at compile time? > 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 >