Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Nullable cast (?int)

Posted by David Rodrigues 
David Rodrigues
[PHP-DEV] Nullable cast (?int)
July 31, 2018 08:30PM
Currently we have support to (int) cast (and similar). But I do think that
we too need a possibility to do a nullable cast. In terms, it will cast to
(int) only if value is not null, else it should be keeped as null.

$number = 123;
(int) $number => 123;
(?int) $number => 123;

$string = '123';
(int) $string => 123;
(?int) $string => 123;

$null = null;
(int) $null => 0
(?int) $null => null

The last example is the biggest problem, because it transforms (int) null
to 0, (bool) null to false, (string) null to '', (array) null to []. It
could be an incorrect behavior when your parameter requires an ?int, where
an int will have a different behavior than null.

Example:

function increase(?int $number): ?int {
if ($number === null) { return null; }
return $number + 1;
}

increase($_POST['number']); // Could causes increase() first argument
should be ?int.
increase((int) $_POST['number']); // Will works, but will be always int,
and if $_POST['number'] is null, then it will fails because will be
converted to 0 erroneous.
increase((?int) $_POST['number']); // Will works if is an int, string or
null, respecting the expected behavior.

--
David Rodrigues
Sara Golemon
Re: [PHP-DEV] Nullable cast (?int)
July 31, 2018 09:10PM
On Tue, Jul 31, 2018 at 2:23 PM, David Rodrigues <[email protected]> wrote:
> Currently we have support to (int) cast (and similar). But I do think that
> we too need a possibility to do a nullable cast. In terms, it will cast to
> (int) only if value is not null, else it should be kept as null.
>
I like this proposal. Short, simple, and consistent with other parts
of PHP syntax. Write up an RFC for it?

-Sara

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Claude Pache
Re: [PHP-DEV] Nullable cast (?int)
July 31, 2018 10:00PM
> Le 31 juil. 2018 à 21:07, Sara Golemon <[email protected]> a écrit :
>
>> On Tue, Jul 31, 2018 at 2:23 PM, David Rodrigues <[email protected]> wrote:
>> Currently we have support to (int) cast (and similar). But I do think that
>> we too need a possibility to do a nullable cast. In terms, it will cast to
>> (int) only if value is not null, else it should be kept as null.
>>
> I like this proposal. Short, simple, and consistent with other parts
> of PHP syntax. Write up an RFC for it?
>
> -Sara
>

And, please, also consider `settype($x, '?int')`.

I have userland functions in my code for those type of casts.

—Claude


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
David Rodrigues
Re: [PHP-DEV] Nullable cast (?int)
August 01, 2018 01:00AM
Thanks a lot!

1. I never did a RFC and have no karma to do, so if someone wants to help
me with that, I appreciate (contact me directly, then we could publish RPC
link as reply here).

2. Too, I could not implementate it, and I don't know if it is a
requirement to write a RFC or if it could be done in another moment.

3. What PHP version it should be focused? 7.4, right?

Em ter, 31 de jul de 2018 às 16:50, Claude Pache <[email protected]>
escreveu:

>
>
> > Le 31 juil. 2018 à 21:07, Sara Golemon <[email protected]> a écrit :
> >
> >> On Tue, Jul 31, 2018 at 2:23 PM, David Rodrigues <
> [email protected]> wrote:
> >> Currently we have support to (int) cast (and similar). But I do think
> that
> >> we too need a possibility to do a nullable cast. In terms, it will cast
> to
> >> (int) only if value is not null, else it should be kept as null.
> >>
> > I like this proposal. Short, simple, and consistent with other parts
> > of PHP syntax. Write up an RFC for it?
> >
> > -Sara
> >
>
> And, please, also consider `settype($x, '?int')`.
>
> I have userland functions in my code for those type of casts.
>
> —Claude
>
>

--
David Rodrigues
Gabriel Caruso
Re: [PHP-DEV] Nullable cast (?int)
August 01, 2018 03:10AM
>
> Thanks a lot!
>
> 1. I never did a RFC and have no karma to do, so if someone wants to help
> me with that, I appreciate (contact me directly, then we could publish RPC
> link as reply here).
>


Someone can give you acess to https://wiki.php.net/rfc to you email, so
you'll be able to write the RFC, no karma need for that.


>
> 2. Too, I could not implementate it, and I don't know if it is a
> requirement to write a RFC or if it could be done in another moment.
>
> 3. What PHP version it should be focused? 7.4, right?
>

Yes, all new features now should target PHP 7.4. Just write the RFC and
open the PR at https://github.com/php/php-src and let the process rollout.

Regards,
--
Gabriel Caruso
Marcos Passos
Re: [PHP-DEV] Nullable cast (?int)
August 01, 2018 04:10AM
Pretty nice idea!

On Tue, Jul 31, 2018 at 22:06 Gabriel Caruso <[email protected]>
wrote:

> >
> > Thanks a lot!
> >
> > 1. I never did a RFC and have no karma to do, so if someone wants to help
> > me with that, I appreciate (contact me directly, then we could publish
> RPC
> > link as reply here).
> >
>
>
> Someone can give you acess to https://wiki.php.net/rfc to you email, so
> you'll be able to write the RFC, no karma need for that.
>
>
> >
> > 2. Too, I could not implementate it, and I don't know if it is a
> > requirement to write a RFC or if it could be done in another moment.
> >
> > 3. What PHP version it should be focused? 7.4, right?
> >
>
> Yes, all new features now should target PHP 7.4. Just write the RFC and
> open the PR at https://github.com/php/php-src and let the process rollout.
>
> Regards,
> --
> Gabriel Caruso
>
David Rodrigues
Re: [PHP-DEV] Nullable cast (?int)
August 02, 2018 03:10AM
Can someone give me permission to publish the RFC?
My username is: david.proweb

Thanks!

Em ter, 31 de jul de 2018 às 23:00, Marcos Passos <
[email protected]> escreveu:

> Pretty nice idea!
>
> On Tue, Jul 31, 2018 at 22:06 Gabriel Caruso <[email protected]>
> wrote:
>
>> >
>> > Thanks a lot!
>> >
>> > 1. I never did a RFC and have no karma to do, so if someone wants to
>> help
>> > me with that, I appreciate (contact me directly, then we could publish
>> RPC
>> > link as reply here).
>> >
>>
>>
>> Someone can give you acess to https://wiki.php.net/rfc to you email, so
>> you'll be able to write the RFC, no karma need for that.
>>
>>
>> >
>> > 2. Too, I could not implementate it, and I don't know if it is a
>> > requirement to write a RFC or if it could be done in another moment.
>> >
>> > 3. What PHP version it should be focused? 7.4, right?
>> >
>>
>> Yes, all new features now should target PHP 7.4. Just write the RFC and
>> open the PR at https://github.com/php/php-src and let the process
>> rollout.
>>
>> Regards,
>> --
>> Gabriel Caruso
>>
>

--
David Rodrigues
Christoph M. Becker
Re: [PHP-DEV] Nullable cast (?int)
August 02, 2018 12:20PM
On 02.08.2018 at 03:02, David Rodrigues wrote:

> Can someone give me permission to publish the RFC?
> My username is: david.proweb

I've granted you RFC karma.

--
Christoph M. Becker

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login