Re: Inline conditional that returns null if falsy

From: Date: Tue, 23 Feb 2021 19:05:47 +0000
Subject: Re: Inline conditional that returns null if falsy
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-113240@lists.php.net to get a copy of this message
On 23/02/2021 18:41, Albert Casademont wrote:
Sure, it's not a big deal having to write the ": null" but it doesn't add any value
On the contrary, it adds an important piece of information: that the default value is "null", rather than "false", or "0", or "new EmptyValue()". For instance, it doesn't seem at all obvious to me that this code should produce a null: $items = []; $itemCount = $items ? count($items); I might be more convinced that "null" is the "natural" value if the left-hand operand was only checked against null, not "falsiness": in that case, you can read it as "if it's null, leave it alone". I'd still be inclined towards "too specific to use up more syntax", though.
A better example would creating UUID VO's from string|null input, there's little room for a default here.
I'm not quite sure what process you're describing, but any of the following seem equally reasonable: $nullableUuid = is_null($input) ? UUID::for($input) : null; $maybeNilUuid = is_null($input) ? UUID::for($input) : UUID:nilValue(); $definitelyUuid = is_null($input) ? UUID::for($input) : UUID:random(); Syntax for skipping the is_null/isset on the left would be higher up my wish list than syntax for skipping the :null on the right. Regards, -- Rowan Tommins [IMSoP]

Thread (23 messages)

« previous php.internals (#113240) next »