Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-11 Thread Mark Randall
On 11/04/2019 08:52, Guilliam Xavier wrote: Honestly as a newbie here I'm feeling more tired than I had expected and I'm not sure if we'll find a syntax+semantics pair that will reach a consensus... Suggestions and comments very welcome! Don't stress yourself about it. At the end of the day

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-11 Thread Guilliam Xavier
OK, I'll try to sum up a bit the recent messages (since Nikita's one): Mark Randall wrote: > As is implied from the name, an implicit cast is done because the code > needs it to, not because it has been told to by the caller. It is > natural to be more conservative when doing something that has

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Mark Randall
On 10/04/2019 22:30, Benjamin Morel wrote: Your arguments are perfectly valid and make sense, I guess this is a matter of not viewing different semantics for explicit and implicit cast as inconsistencies. It's an interesting discussion and I appreciate you engaging on it. I wonder, perhaps,

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Benjamin Morel
> That's exactly what it is, and thanks to null coalescence, you then have > an easy available ability to either check if it succeeded, or default to > another value. Your arguments are perfectly valid and make sense, I guess this is a matter of not viewing different semantics for explicit and

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Mark Randall
On 10/04/2019 18:34, Benjamin Morel wrote: So why would you have different semantics for implicit `(?int)` cast vs `: ?int` function return type cast, if they're both *out*? Return type cast has the same semantics as parameter type cast. I would have to disagree with this as I think of

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Benjamin Morel
> Function arguments specify what goes *in* to a function, whereas casts > and return types specify what comes *out* of the function. So why would you have different semantics for implicit `(?int)` cast vs `: ?int` function return type cast, if they're both *out*? Return type cast has the same

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Mark Randall
On 10/04/2019 17:15, Benjamin Morel wrote: No offense, but how can casting semantics different from those already used in implicit casting be intuitive? As is implied from the name, an implicit cast is done because the code needs it to, not because it has been told to by the caller. It is

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Benjamin Morel
> > I believe returning null in those situations makes the most intuative > sense, yes. No offense, but how can casting semantics different from those already used in implicit casting be intuitive? function a(int $x) { var_export($x); } function b(?int $x) { var_export($x); }

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Mark Randall
On 10/04/2019 16:16, Guilliam Xavier wrote: var_dump((?int) $obj); // would you want it to print `NULL`? var_dump((?string) $obj); // would you want it to print `NULL`? I believe returning null in those situations makes the most intuative sense, yes. > var_dump((int) $obj); // Notice, then

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Guilliam Xavier
On Wed, Apr 10, 2019 at 4:34 PM Mark Randall wrote: > > (Continuing from my previous post in a different thread...) > > IMHO a cast such as ([?]type) would be better described as passing a > mixed value through a function which has a return type of whatever is in > the cast... the mechanics of

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Guilliam Xavier
On Wed, Apr 10, 2019 at 12:52 PM Nikita Popov wrote: > > I'm a bit concerned that there are two possible semantics for what (?int) > does: > > 1. What I would intuitively expect: A fallible integer cast. I.e. try to > cast to integer and if not possible return null. So (?int) "foobar" becomes >

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Mark Randall
On 10/04/2019 13:03, Benjamin Morel wrote: There is only one, if we align the semantics of explicit and implicit casting: you wouldn't expect this: function test(?int $bar) { var_export($bar); } test("abc"); ...to output 'null'? (Continuing from my previous post in a different

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Benjamin Morel
> > I'm a bit concerned that there are two possible semantics for what (?int) > does: There is only one, if we align the semantics of explicit and implicit casting: you wouldn't expect this: function test(?int $bar) { var_export($bar); } test("abc"); ...to output 'null'? -- Ben

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-10 Thread Nikita Popov
On Sat, Apr 6, 2019 at 9:53 AM Guilliam Xavier wrote: > Hello internals, > > David and I would like to open the discussion on our joint RFC: > > https://wiki.php.net/rfc/nullable-casting > > Mainly, it would enable to use e.g. `(?int)$x` besides `(int)$x`. > > We are looking forward to your

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-09 Thread Lester Caine
On 09/04/2019 08:32, Guilliam Xavier wrote: I do kind of like the idea in the RFC, but I think it needs a better argument for it. For your other points, I find that Claude Pache has given pretty good arguments in the meantime (thanks!) =) (By the way, I also thought about say,

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-09 Thread Guilliam Xavier
Dan Ackroyd wrote: > > 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? Hello, thanks for digging in :) Here's a "real-world" situation I

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-09 Thread Guilliam Xavier
On Mon, Apr 8, 2019 at 11:55 PM M. W. Moe wrote: > > [redacted] > > On Mon, Apr 8, 2019 at 7:44 AM Dan Ackroyd wrote: > > > On Mon, 8 Apr 2019 at 04:21, M. W. Moe wrote: > > > > > > [redacted] > > > > [redacted] > > As the initiator of this thread (who hasn't had the time to answer questions

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread M. W. Moe
Hello, i) was intentional. ii) Not our fault, we do not have this function in our code-base and won't ever; we would not even dare 8). Cheers! On Mon, Apr 8, 2019 at 7:44 AM Dan Ackroyd wrote: > On Mon, 8 Apr 2019 at 04:21, M. W. Moe wrote: > > > > Hello, > > > > looks like you are living

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Pedro Magalhães
On Mon, Apr 8, 2019 at 6:05 AM David Rodrigues wrote: > Current solution: > > $itemsPerPage = Input::get('itemsPerPage') ?: null; > $itemsPerPage = $itemsPerPage !== null ? (int) $itemsPerPage : null; > > $paginator->setItemsPerPage($itemsPerPage); // OK > > With this new feature: just... > >

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Claude Pache
> Le 8 avr. 2019 à 07:05, David Rodrigues a écrit : > > And about "settype($variable, "?int")" it was requested on original mailing > by Claude Pache. PHP has several ways to perform casting: 1. (int) $foo 2. settype($foo, 'int'); 3. intval($foo) They are occasions where the programmer will

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Claude Pache
> Le 8 avr. 2019 à 12:24, Stephen Reay a écrit : > > if your target ’type’ is `?int` and you pass an empty string, wouldn’t you > expect to get `null` rather than `0` ? You should get the same value than you get in: ```php declare(strict_types=0); function foo(?int $x) { return $x;

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Dan Ackroyd
On Mon, 8 Apr 2019 at 06:05, David Rodrigues wrote: > > Question: why just not check if $itemsPerPage is set like: > ... > Answer: because in this example we could do that (although the new solution > is much more practical). Well, I disagree on that. That code is perfectly fine to me, and

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Dan Ackroyd
On Mon, 8 Apr 2019 at 04:21, M. W. Moe wrote: > > Hello, > > looks like you are living in a pig stall; php would be benefit from type > safety when explicitly required. > > Regards. Hi Moe, It seems you: i) accidentally hit reply, instead of reply all. This is an easy mistake to make. ii)

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Benjamin Morel
> if your target ’type’ is `?int` and you pass an empty string, wouldn’t you expect to get `null` rather than `0` ? I'd personally expect an `Error`, but this is not very much in line with current casting semantics. You have a point here, there is no clear answer for this edge case. Maybe

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Stephen Reay
> On 8 Apr 2019, at 16:35, Benjamin Morel wrote: > >> register_cast_function('?int', 'castToIntOrNull'); > > 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. > > I

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-08 Thread Benjamin Morel
> register_cast_function('?int', 'castToIntOrNull'); 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. I quite like the idea of nullable casting though, as I've come across a

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-07 Thread David Rodrigues
Real world example: class Paginator { // If null, will use the default value (eg. 15), else, it will requires an int. public function setItemsPerPage(?int $itemsPerPage) { ... } } // Info that came from user input. // It will be a string, and not a integer, like we need. $itemsPerPage =

Re: [PHP-DEV] [RFC] Nullable Casting

2019-04-07 Thread Dan Ackroyd
On Sat, 6 Apr 2019 at 08:53, Guilliam Xavier wrote: > > Hello internals, > > David and I would like to open the discussion on our joint RFC: > > https://wiki.php.net/rfc/nullable-casting > > Mainly, it would enable to use e.g. `(?int)$x` besides `(int)$x`. > I'm guessing you don't actually have

[PHP-DEV] [RFC] Nullable Casting

2019-04-06 Thread Guilliam Xavier
Hello internals, David and I would like to open the discussion on our joint RFC: https://wiki.php.net/rfc/nullable-casting Mainly, it would enable to use e.g. `(?int)$x` besides `(int)$x`. We are looking forward to your feedback and, if possible, help to fill in the "RFC Impact" subsections