On Wed, Jun 26, 2024, at 23:42, Ilija Tovilo wrote:
> 
> function test($value) {
>     if ($value is ['foo' => ?string]) {
>         $foo = $value['foo'];
>     }
> }
> test([]);

(Scroll to the end if you don’t care about my rebuttal)

I mean, there’s nothing wrong with this code, $foo will be null at the end.

> 
> With your approach, the example above would emit a warning, even
> though the context within test() doesn't look like it should. 

That happens every time I get one of these warnings. It’s nice to get the heads 
up that I didn’t think through something and need to either A) figure out how 
it got there, or B) provide a default — even if it is null — to make the 
warning go away. 

> If ?string means that the index might or might not exist, all code that
> accesses them must check for existence, even when not needing to
> handle null itself. That doesn't seem desirable to me.

This is true already. since this warning and null-coalescence, I can probably 
count on one hand the number of times I’ve *not* written something like ?? null 
beside an array access (though I prefer throwing an exception if appropriate). 

> 
> I also think the issue goes further. If anything|null means that the
> offset might not exist, does that include mixed? That makes ['foo' =>
> mixed] essentially useless.

In thinking about it some more, for lists, it would be nice if there were two 
modes:
 1. regular mode: $a is [1,2,3] where it must match exactly. 
 2. set mode: $a is set([1,2,3]) where order doesn’t matter, only that $a 
contains at least one of every given element. 
——————

In other news, I finally found a realistic example where there is a difference 
between non-existence and null.

The scenario is this:

Imagine you accept a callback and you prepare an array to pass as parameters. 
In this case, you call the callback like so:

$callback(…$args);

Today, you either have to yolo it and hope everything is perfect or 
meticulously check the args to make sure the types match your documented 
callback signature.

If a key in $args doesn’t exist, it might be an error (or default value 
provided by the callback implementation) but if it is null, it might also be 
disastrously incorrect because the default value won’t be used. 

By using pattern matching, we can check the structure of the array for 
non-existence or if exists, the documented type. This is where matching a list 
exactly is important (if using positional args).

So, now that there is a use case for it, we can work out expected behaviors of 
lists and associative arrays and nullability for both.

Anyway, does anyone have a suggestion for what kind of hat I should have for 
dinner?

— Rob

Reply via email to