digitalmars.D.learn - Why double not? (!!)
- Ryan (4/6) Nov 18 2016 Why do I see double `not` operators sometimes in D code? An
- Xinok (5/11) Nov 18 2016 It's a more concise way of writing:
- Era Scarecrow (8/22) Nov 18 2016 Hmmm... thinking about it, it does make perfect sense. The first
- Is it possible to store different generic types? (15/39) Nov 18 2016 It's a very common practice in any language that uses
- Is it possible to store different generic types? (8/49) Nov 18 2016 It's especially difficult with numbers like you did point out and
- Ryan (4/13) Nov 19 2016 Wouldn't this just be the same as
- Adam D. Ruppe (7/11) Nov 19 2016 Yes, it is in D, though the habit often comes from C where things
- Patrick Schluter (13/20) Nov 19 2016 Yes it is a C idiom to coerce the boolean value to be 0 or 1.
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.orgimport core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();Thanks.
Nov 18 2016
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.orgIt's a more concise way of writing: GetConsoleCP() != 0 You can do this in C/C++ as well (and presumably some other languages).import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();Thanks.
Nov 18 2016
On Saturday, 19 November 2016 at 04:54:22 UTC, Xinok wrote:On Saturday, 19 November 2016 at 03:52:02 UTC, Ryan wrote: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.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.orgIt's a more concise way of writing: GetConsoleCP() != 0 You can do this in C/C++ as well (and presumably some other languages).import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();Thanks.
Nov 18 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: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) { }On Saturday, 19 November 2016 at 03:52:02 UTC, Ryan wrote: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.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.orgIt's a more concise way of writing: GetConsoleCP() != 0 You can do this in C/C++ as well (and presumably some other languages).import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();Thanks.
Nov 18 2016
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: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.000001 would be true, -0.000000001 would be true.On Saturday, 19 November 2016 at 04:54:22 UTC, Xinok wrote: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) { }On Saturday, 19 November 2016 at 03:52:02 UTC, Ryan wrote: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.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.orgIt's a more concise way of writing: GetConsoleCP() != 0 You can do this in C/C++ as well (and presumably some other languages).import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();Thanks.
Nov 18 2016
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.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.
Nov 19 2016
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.
Nov 19 2016
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: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).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.
Nov 19 2016