Re: Why double not? (!!)
On Saturday, 19 November 2016 at 15:50:26 UTC, Adam D. Ruppe wrote: On Saturday, 19 November 2016 at 15:40:38 UTC, Ryan wrote: Wouldn't this just be the same as auto hasConsole = cast(bool) GetConsoleCP(); ? Yes, it is in D, though the habit often comes from C where things are different. But people also may prefer !! for just being shorter and once you know the pattern, you'll see them as meaning the same thing. Yes it is a C idiom to coerce the boolean value to be 0 or 1. Since C99 C has the built in type bool which will also coerce the value. bool hasConsole = (bool)GetConsoleCP(); will do. If the code is to be compiled on a pre C99 compiler then it is problematic as the value depend on the definition of the type bool. In that case you have to use the !! or the !=0 idiom. That's why C code still often use them because of the risk of being compiled on an old compiler (and Microsoft doesn't implement C99 anyway in MSVC, so for real portable code it's kind of required).
Re: Why double not? (!!)
On Saturday, 19 November 2016 at 15:40:38 UTC, Ryan wrote: Wouldn't this just be the same as auto hasConsole = cast(bool) GetConsoleCP(); ? Yes, it is in D, though the habit often comes from C where things are different. But people also may prefer !! for just being shorter and once you know the pattern, you'll see them as meaning the same thing. I think the GetConsoleCP() != 0 code is the clearest about your intentions. yeah that's my preference too. But they still mean the same thing so you get used to all the forms.
Re: Why double not? (!!)
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. Wouldn't this just be the same as auto hasConsole = cast(bool) GetConsoleCP(); ? I think the GetConsoleCP() != 0 code is the clearest about your intentions.
Re: Why double not? (!!)
On Saturday, 19 November 2016 at 07:51:36 UTC, Is it possible to store different generic types? wrote: 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/ktlpnikvdwgbvfaam...@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) { } It's especially difficult with numbers like you did point out and it completely depends on languages. Most languages have false when it's 0, null, undefined etc. and everything else is true. Which means -1 would be true, 0 would be false, 1 would be true, 0.01 would be true, -0.1 would be true.
Re: Why double not? (!!)
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/ktlpnikvdwgbvfaam...@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) { }
Re: Why double not? (!!)
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/ktlpnikvdwgbvfaam...@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.
Re: Why double not? (!!)
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/ktlpnikvdwgbvfaam...@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).
Why double not? (!!)
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/ktlpnikvdwgbvfaam...@forum.dlang.org import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP(); Thanks.