On Tue, Feb 23, 2021, at 10:49 AM, Albert Casademont wrote: > Another example is when a scalar input needs to be either left null or > converted to a Value Object: > > $color = $data['color'] ? new Color($data['color']) : null; > > This would become; > > $color = $data['color'] ? new Color($data['color']); //the ": null" part is > implicit > > It's actually kinda the inverse of the "?:" operator. > > As Guilliam said, Twig already implements such behaviour, it's quite handy > when dealing with nullable variables,
1) Please don't top post. 2) The advantage of ?: over long-ternary is that the part it lets you omit is of variable size, and is often verbose (nested array elements). That's not the case here, as the omitted portion is a fixed length short constant value (": null"). So the value of the abbreviation is much less. I am also not a fan of null being commonly used, as it is a dangerous value. More often I would not want null if the color were missing but some default color value, which would be represented by something other than null anyway. And "falsy" is, as we've been reminded numerous times, a dangerous and tricky thing. (I just had several PRs against one of my OSS libraries because it relied on falsy, which had all sorts of incorrect failure conditions.) I'd be a -1 here. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php