Project Elvis
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Oct 29 16:29:57 UTC 2017
On Sunday, October 29, 2017 16:18:43 Ola Fosheim Grøstad via Digitalmars-d
wrote:
> On Sunday, 29 October 2017 at 14:37:57 UTC, Jonathan M Davis
>
> wrote:
> > everything, but I could have missed something). As proposed
> > thus far, the Elvis operator is just the ternary operator where
> > the condition is reused as the first branch, so having it work
> > with opCast fits in perfectly with everything else.
>
> I understand that it is being defined as a short hand for the
> ordinary ternary operator, but if you look at the use context the
> elvis-operator tend to be used to filter out invalid state.
>
> So if casting to bool would test for validity then it would make
> more sense, but that is not the general case...
>
> > What would you be looking to do that does not fit in with this?
>
> I think it would be better to have a test for validity and use
> that for substituting in default values (which is what the elvis
> operator is typically used for).
And what does testing for validity even mean? Doesn't that depend on the
type? I would argue that with regards to the built-in types what cast(bool)
does for them is as close to checking for validity as you're going to get,
and for user-defined types, they can then just overload opCast!bool to mean
whatever they want so long as the result is true or false. In general, if
you're looking to check whether something is valid using ?:, I would think
that you'd want to be doing the same check with stuff like if statements
anyway. So, it sounds to me like overloading opCast!bool would work just
fine.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list