On 27/01/2026 12:13, Rob Landers wrote:

I kinda like this idea, but it would be nice to follow the same rules we have with function calls, thus

int|null $var = "123test"; (https://3v4l.org/C0uaK/rfc#vgit.master)


This comes back to my point about the two-part definition.

You are suggesting that "int|null" should only affect the *input interpretation*: "allow null as a valid input, but error on invalid input".

Lynn is suggesting "int|null" should also affect the *output behaviour*: "use null as the default output for any invalid input".


This is why I keep thinking about a *family* of cast operations, where the *input interpretation* is standardised, but the *output behaviour* is selectable by the user.

My current best thought is a generic-style syntax:

must_cast<int>( 'hello' ) // TypeError
try_cast<int>( 'hello', 0 ) // int(0)
can_cast<int>( 'hello' ) // bool(false)

must_cast<int>( null ) // TypeError
try_cast<int>( null, 0 ) // int(0)
can_cast<int>( null ) // bool(false)

must_cast<int|null>( 'hello' ) // TypeError
try_cast<int|null>( 'hello', null ) // null
can_cast<int|null>( 'hello' ) // bool(false)

must_cast<int|null>( null ) // null
try_cast<int|null>( null, null ) // null
can_cast<int|null>( null ) // bool(true)


--
Rowan Tommins
[IMSoP]

Reply via email to