Why double not? (!!)
Is it possible to store different generic types? via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Nov 18 23:51:36 PST 2016
On Saturday, 19 November 2016 at 06:58:38 UTC, Era Scarecrow
wrote:
> On Saturday, 19 November 2016 at 04:54:22 UTC, Xinok wrote:
>> On Saturday, 19 November 2016 at 03:52:02 UTC, Ryan wrote:
>>> Why do I see double `not` operators sometimes in D code? An
>>> example it the last post of this thread.
>>>
>>> http://forum.dlang.org/thread/ktlpnikvdwgbvfaamrsk@forum.dlang.org
>>>
>>>> import core.sys.windows.windows : GetConsoleCP;
>>>> bool hasConsole = !!GetConsoleCP();
>>>
>>> Thanks.
>>
>> It's a more concise way of writing:
>> GetConsoleCP() != 0
>>
>> You can do this in C/C++ as well (and presumably some other
>> languages).
>
> Hmmm... thinking about it, it does make perfect sense. The
> first ! converts it to bool, the other inverts it back to it's
> positive/negative state.
>
> Although it's a combination of logic I wouldn't have through
> of unless I saw it. But testing the result on any number
> (float, double or real) won't be precise and would take far
> longer (and more complicated) using another method.
It's a very common practice in any language that uses
truthy/falsey, especially seen a lot in Javascript.
Generally it's not necessary unless you want to be explicit about
checking upon a bool.
Ex.
auto hasModel = !!view.model;
if (hasModel) {
...
}
Could very well just be
auto model = view.model;
if (model) {
}
More information about the Digitalmars-d-learn
mailing list