Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105360 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15902 invoked from network); 23 Apr 2019 16:57:18 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 23 Apr 2019 16:57:18 -0000 To: internals@lists.php.net References: <9fee0f79-a77d-c0f1-ec24-efa4dd587f91@hristov.com> Date: Tue, 23 Apr 2019 14:57:45 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; 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-GB Content-Transfer-Encoding: 7bit X-Posted-By: 94.0.205.114 Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: markyr@gmail.com (Mark Randall) Message-ID: On 23/04/2019 14:39, Mark Randall wrote: > It keeps coming back to the feature we've all been praying to the PHP > Gods for... generics. Replying to myself (sorry). I just wanted to point out that the same syntax would also solve your other issue from the nullable casts thread: function ncast($value) ?T { if ($value === null) { return null; } if (settype($value, T) === false) { throw new InvalidArgumentException('Cannot convert to ' . T); } return $value; } ncast(null); ncast(12345); ncast(null); ncast('abcdef'); -- Mark Randall