On Wed, Mar 20, 2024 at 8:30 PM Rowan Tommins [IMSoP]
<imsop....@rwec.co.uk> wrote:
>
>
>
> On 20 March 2024 12:51:15 GMT, Robert Landers <landers.rob...@gmail.com> 
> wrote:
>
> >Oh and there isn't any difference between:
> >
> >$x as ?Type
> >
> >or
> >
> >$x as Type|null
>
>
> I'm not sure if I've misunderstood your example, or you've misunderstood mine.
>
> I'm saying that this should be an error, because the value is neither an 
> instance of Foo nor null:
>
> $a = 42;
> $b = $a as Foo|null;
>
> Your earlier example implies that would make $b equal null, which feels wrong 
> to me, because it means it wouldn't match this:
>
> $a = 42;
> $b = $a as Foo|Bar;
>
> If we want a short-hand for "set to null on error" that should be separate 
> from the syntax for a nullable type.
>
>
> Regards,
> Rowan Tommins
> [IMSoP]

Interesting. I'm not sure there's a better way to say "set to null on
error" since it would be barely sensical to give a nullable type on
the right hand side anyway; so we might as well use it. In other
words, I can't think of a case where you'd actually want a Type|null
and you wouldn't have to check for null anyway.

If you think about it, in this proposal, you could use it in a match:

// $a is TypeA|TypeB|null

match (true) {
  $a as ?TypeA => 'a',
  $a as ?TypeB => 'b',
  $a === null => 'null',
}

No matter what, you're going to have to check for null if you want to
handle all cases and throwing an error when you ask for a nullable
type would render a ton of utility moot (i.e., this match statement
wouldn't work). It's probably better to say, "I want what is on the
right-hand side of `as` or an error." Including `null` in that type
seems to be that you would get null if no other type matches, since
any variable can be `null`.

Robert Landers
Software Engineer
Utrecht NL

Reply via email to