On Tue, Feb 23, 2021 at 6:28 PM Larry Garfield <la...@garfieldtech.com>
wrote:

> 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).


For completeness: it's also important when implying side effects, e.g.
`foo() ?: bar()` calls foo() only once, while `foo() ? foo() : bar()` may
twice.

  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.
>

Indeed, and (as I said too) I'm not even sure whether it improves
readability or instead worsens it.
(By the way, I mentioned Twig because David's HTML example (Smarty maybe?)
reminded me of it, but that's a "template engine"; I actually don't know a
"general purpose" programming language with a C-like ternary operator that
allows omitting the ": null" part.)


>
> 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.


You seem to join Marco here ;)

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 would just add that it's also true for "?:" (i.e. not specific to the
suggestion at hand, although the lack of a non-controversial example so far
may be a hint)


>
> I'd be a -1 here.
>
> --Larry Garfield
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
-- 
Guilliam Xavier

Reply via email to