Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Robert Landers
On Fri, Mar 22, 2024 at 8:02 PM Rowan Tommins [IMSoP] wrote: > > On Fri, 22 Mar 2024, at 17:38, Claude Pache wrote: > > > Le 22 mars 2024 à 16:18, Rowan Tommins [IMSoP] a écrit > : > > $optionalExpiryDateTime = $expiry as ?DateTimeInterface else > some_other_function($expiry); >

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Robert Landers
On Fri, Mar 22, 2024 at 5:51 PM Rowan Tommins [IMSoP] wrote: > > On Fri, 22 Mar 2024, at 12:58, Robert Landers wrote: > > > >> $optionalExpiryDateTime = $expiry as ?DateTimeInterface else new > >> DateTimeImmutable($expiry); > > I'm not sure I can grok what this does... > > > >

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Rowan Tommins [IMSoP]
On Fri, 22 Mar 2024, at 17:38, Claude Pache wrote: > >> Le 22 mars 2024 à 16:18, Rowan Tommins [IMSoP] a >> écrit : >> >> $optionalExpiryDateTime = $expiry as ?DateTimeInterface else >> some_other_function($expiry); >> assert($optionalExpiryDateTime is ?DateTimeInterface); // cannot fail, >>

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Claude Pache
> Le 22 mars 2024 à 16:18, Rowan Tommins [IMSoP] a écrit > : > > $optionalExpiryDateTime = $expiry as ?DateTimeInterface else > some_other_function($expiry); > assert($optionalExpiryDateTime is ?DateTimeInterface); // cannot fail, > already asserted by the "as" I think that the `is`

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Rowan Tommins [IMSoP]
On Fri, 22 Mar 2024, at 12:58, Robert Landers wrote: > >> $optionalExpiryDateTime = $expiry as ?DateTimeInterface else new >> DateTimeImmutable($expiry); > I'm not sure I can grok what this does... > > $optionalExpiryDateTime = ($expiry === null || $expiry instanceof > DateTimeInterface) ?

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Jordi Boggiano
On 2024-03-22 10:46, Rowan Tommins [IMSoP] wrote: On Fri, 22 Mar 2024, at 08:17, Jordi Boggiano wrote: We perhaps could make sure that as does not throw if used with `??`, or that `??` catches the type error and returns the right-hand expression instead: So to do a nullable typecast you

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Robert Landers
On Fri, Mar 22, 2024 at 12:01 PM Rowan Tommins [IMSoP] wrote: > > On Fri, 22 Mar 2024, at 08:17, Jordi Boggiano wrote: > > We perhaps could make sure that as does not throw if used with `??`, or that > `??` catches the type error and returns the right-hand expression instead: > > So to do a

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Rowan Tommins [IMSoP]
On Fri, 22 Mar 2024, at 10:05, Robert Landers wrote: > After asking an AI for some examples and usages, the most compatible > one would be C#'s. In actuality, I think it could be hugely simplified > if we simply return null instead of throwing. There'd be no special > case for |null, and it would

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Robert Landers
On Fri, Mar 22, 2024 at 10:31 AM Jordi Boggiano wrote: > > On 2024-03-21 16:02, Robert Landers wrote: > > $a as int|float > > would be an int, float, or thrown exception. > > $a as int|float|null > > would be an int, float, or null. > > Just a suggestion here which might be more palatable to

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Rowan Tommins [IMSoP]
On Fri, 22 Mar 2024, at 08:17, Jordi Boggiano wrote: > We perhaps could make sure that as does not throw if used with `??`, or that > `??` catches the type error and returns the right-hand expression instead: > So to do a nullable typecast you would do: > > $a as int|float ?? null > While

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Jordi Boggiano
On 2024-03-21 16:02, Robert Landers wrote: $a as int|float would be an int, float, or thrown exception. $a as int|float|null would be an int, float, or null. Just a suggestion here which might be more palatable to Rowan's wish for consistency (which I can totally relate to): We perhaps

Re: [PHP-DEV] Proposal: AS assertions

2024-03-22 Thread Rowan Tommins [IMSoP]
On 22 March 2024 00:04:27 GMT, Robert Landers wrote: >I think that is where we are getting confused: `null` is a value (or >at least, the absence of a value). The fact that the type system >allows it to be used as though its a type (along with true and false) >is interesting, but I think it

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Robert Landers
On Thu, Mar 21, 2024 at 11:06 PM Rowan Tommins [IMSoP] wrote: > > On 21/03/2024 19:03, Robert Landers wrote: > > I suppose we are taking this from different viewpoints, yours appears > to be more of a philosophical one, whereas mine is more of a practical > one. > > > My main concern is

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Matthew Brown
> > What's the advantage of a language construct over the following? > > ```php > /** > * @template T of object > * @psalm-assert T $value > * @param class-string $type > */ > function as(mixed $value, string $type): mixed > { > if (! $value instanceof $type) { throw >

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Rowan Tommins [IMSoP]
On 21/03/2024 19:03, Robert Landers wrote: I suppose we are taking this from different viewpoints, yours appears to be more of a philosophical one, whereas mine is more of a practical one. My main concern is consistency; which is partly philosophical, but does have practical impact - the

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Robert Landers
On Thu, Mar 21, 2024 at 7:01 PM Rowan Tommins [IMSoP] wrote: > > On 21/03/2024 15:02, Robert Landers wrote: > > I don't think you are getting what I am saying. > > $a as int|float > > would be an int, float, or thrown exception. > > $a as int|float|null > > would be an int, float, or null. > > >

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Rowan Tommins [IMSoP]
On 21/03/2024 15:02, Robert Landers wrote: I don't think you are getting what I am saying. $a as int|float would be an int, float, or thrown exception. $a as int|float|null would be an int, float, or null. I get what you're saying, but I disagree that it's a good idea. If $a is 'hello',

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Larry Garfield
On Thu, Mar 21, 2024, at 3:02 PM, Robert Landers wrote: > I don't think you are getting what I am saying. > > $a as int|float > > would be an int, float, or thrown exception. > > $a as int|float|null > > would be an int, float, or null. > > Robert Landers > Software Engineer > Utrecht NL Hi Rob.

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Robert Landers
On Thu, Mar 21, 2024 at 12:45 PM Rowan Tommins [IMSoP] wrote: > > On 20/03/2024 23:05, Robert Landers wrote: > > 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. > > > It's not about having to check for null; it's

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Robert Landers
On Thu, Mar 21, 2024 at 12:45 PM Rowan Tommins [IMSoP] wrote: > > On 20/03/2024 23:05, Robert Landers wrote: > > 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. > > > It's not about having to check for null; it's

Re: [PHP-DEV] Proposal: AS assertions

2024-03-21 Thread Rowan Tommins [IMSoP]
On 20/03/2024 23:05, Robert Landers wrote: > 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. It's not about having to check for null; it's about being able to distinguish between "a null value, which was one of

Re: [PHP-DEV] Proposal: AS assertions

2024-03-20 Thread Robert Landers
On Wed, Mar 20, 2024 at 8:30 PM Rowan Tommins [IMSoP] wrote: > > > > On 20 March 2024 12:51:15 GMT, Robert Landers > 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

Re: [PHP-DEV] Proposal: AS assertions

2024-03-20 Thread Rowan Tommins [IMSoP]
On 20 March 2024 12:51:15 GMT, Robert Landers 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

Re: [PHP-DEV] Proposal: AS assertions

2024-03-20 Thread Robert Landers
On Wed, Mar 20, 2024 at 1:47 PM Robert Landers wrote: > > On Tue, Mar 19, 2024 at 10:06 PM Rowan Tommins [IMSoP] > wrote: > > > > On 19/03/2024 16:24, Robert Landers wrote: > > > > $x = $attributeReflection->newInstance() as ?MyAttribute; > > if ($x === null) // do something since the attribute

Re: [PHP-DEV] Proposal: AS assertions

2024-03-20 Thread Robert Landers
On Tue, Mar 19, 2024 at 10:06 PM Rowan Tommins [IMSoP] wrote: > > On 19/03/2024 16:24, Robert Landers wrote: > > $x = $attributeReflection->newInstance() as ?MyAttribute; > if ($x === null) // do something since the attribute isn't MyAttribute > > > I think reusing nullability for this would be a

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Ilija Tovilo
Hi Rowan On Tue, Mar 19, 2024 at 8:39 PM Rowan Tommins [IMSoP] wrote: > > As well pattern matching, which Ilija mentioned, another adjacent feature is > a richer set of casting operators. Currently, we can assert that something is > an int; or we can force it to be an int; but we can't easily

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Rowan Tommins [IMSoP]
On 19/03/2024 16:24, Robert Landers wrote: $x = $attributeReflection->newInstance() as ?MyAttribute; if ($x === null) // do something since the attribute isn't MyAttribute I think reusing nullability for this would be a mistake - ideally, the right-hand side should allow any type, so "$foo

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Ilija Tovilo
Hi Marco On Tue, Mar 19, 2024 at 7:04 PM Marco Aurélio Deleu wrote: > > > On 19 Mar 2024, at 14:51, Ilija Tovilo wrote: > > > > Hi Robert > > > >> On Tue, Mar 19, 2024 at 5:24 PM Robert Landers > >> wrote: > >> > > See https://wiki.php.net/rfc/pattern-matching#throwing_alternative. I > >

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Marco Aurélio Deleu
Marco Deleu > On 19 Mar 2024, at 14:51, Ilija Tovilo wrote: > > Hi Robert > >> On Tue, Mar 19, 2024 at 5:24 PM Robert Landers >> wrote: >> >> I've been thinking about this as an RFC for awhile, but with generics >> being far off (if at all), I'd like to propose a useful idea: reusing >>

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Marco Pivetta
On Tue, 19 Mar 2024 at 17:46, Deleu wrote: > On Tue, Mar 19, 2024 at 1:42 PM Marco Pivetta wrote: > >> One note: if what you are going for is what `azjezz/psl`, be aware that >> exception / error tracing design needs special attention here: it's not as >> simple as it looks! >> > > I believe

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Ilija Tovilo
Hi Robert On Tue, Mar 19, 2024 at 5:24 PM Robert Landers wrote: > > I've been thinking about this as an RFC for awhile, but with generics > being far off (if at all), I'd like to propose a useful idea: reusing > the AS keyword in a different context. > > Example: > > $x =

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Deleu
On Tue, Mar 19, 2024 at 1:42 PM Marco Pivetta wrote: > One note: if what you are going for is what `azjezz/psl`, be aware that > exception / error tracing design needs special attention here: it's not as > simple as it looks! > I believe you answered your own question here. The proposal seems

Re: [PHP-DEV] Proposal: AS assertions

2024-03-19 Thread Marco Pivetta
Hey Robert, On Tue, 19 Mar 2024 at 17:24, Robert Landers wrote: > Hello internals, > > I've been thinking about this as an RFC for awhile, but with generics > being far off (if at all), I'd like to propose a useful idea: reusing > the AS keyword in a different context. > > Example: > > $x =