Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105143 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95967 invoked from network); 8 Apr 2019 13:28:28 -0000 Received: from unknown (HELO mail1.25mail.st) (206.123.115.54) by pb1.pair.com with SMTP; 8 Apr 2019 13:28:28 -0000 Received: from [10.0.1.6] (unknown [49.48.245.84]) by mail1.25mail.st (Postfix) with ESMTPSA id 11A7B60481; Mon, 8 Apr 2019 10:24:51 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) In-Reply-To: Date: Mon, 8 Apr 2019 17:24:47 +0700 Cc: Dan Ackroyd , Guilliam Xavier , PHP internals , David Rodrigues Content-Transfer-Encoding: quoted-printable Message-ID: <5F014F9A-5BBD-4DED-B490-05FC681DE2BF@koalephant.com> References: To: Benjamin Morel X-Mailer: Apple Mail (2.3445.104.8) Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: php-lists@koalephant.com (Stephen Reay) > On 8 Apr 2019, at 16:35, Benjamin Morel = wrote: >=20 >> register_cast_function('?int', 'castToIntOrNull'); >=20 > I would quite oppose such a generic implementation, as it might have > unexpected consequences in other parts of the codebase : it's a global > configuration set at runtime. >=20 > I quite like the idea of nullable casting though, as I've come across = a few > cases myself where this would have been useful and would have made the = code > more concise/readable. >=20 > Ben >=20 > On Mon, 8 Apr 2019 at 04:55, Dan Ackroyd = wrote: >=20 >> On Sat, 6 Apr 2019 at 08:53, Guilliam Xavier = >> wrote: >>>=20 >>> Hello internals, >>>=20 >>> David and I would like to open the discussion on our joint RFC: >>>=20 >>> https://wiki.php.net/rfc/nullable-casting >>>=20 >>> Mainly, it would enable to use e.g. `(?int)$x` besides `(int)$x`. >>>=20 >>=20 >> I'm guessing you don't actually have ths function getIntOrNull() in >> your code-base? To help me understand where this would be useful, >> could you provide some 'real-world' code where this would be useful? >>=20 >> By the way, this RFC is a special case of something that could be far >> more generic. If it was possible to register callbacks to be used = when >> casting, people could do something like this: >>=20 >> function castToIntOrNull($value) >> { >> if ($value =3D=3D=3D null) { >> return null; >> } >>=20 >> return (int)$int; >> } >>=20 >> register_cast_function('?int', 'castToIntOrNull'); >>=20 >> $x =3D (?int)getIntOrNull(); >>=20 >>=20 >>> Additionally, it was requested on the mailing list to consider = adding >>> support of nullable types to the settype() function, >>> e.g. settype($variable, "?int") >>=20 >> Someone probably needs to make an argument for it to be in core, >> rather than just saying that it's something that could be done. >>=20 >> cheers >> Dan >> Ack >>=20 >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >>=20 >>=20 I=E2=80=99ve come across this pattern (string variable contains a number = or nothing) myself, but it also then makes me think - if your target = =E2=80=99type=E2=80=99 is `?int` and you pass an empty string, = wouldn=E2=80=99t you expect to get `null` rather than `0` ?=20