www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The Is Operator

reply "Kyle G." <kyle.james.gibson gmail.com> writes:
Hi,

First time posting here on the NG, and certainly not my last.

D has a good deal of syntactic sugar which results in easier to read 
code. However, there's one particular thing that has been bothering me 
for a while now:

void foo(void[] var) {
  if (var !is null)
   // line of code
}

int main() {
  foo(null);
}

The code I am concerned about is "var !is null" which appears to 
translate to "var not is null" when it actually means "var is not null." 
Is there any special reason why we are unable to do "var isnot null" or 
"var is not null"?
Oct 02 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Kyle G. wrote:
 Hi,
 
 First time posting here on the NG, and certainly not my last.
 
 D has a good deal of syntactic sugar which results in easier to read 
 code. However, there's one particular thing that has been bothering me 
 for a while now:
 
 void foo(void[] var) {
  if (var !is null)
   // line of code
 }
 
 int main() {
  foo(null);
 }
 
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
Only reason is that it would require adding an extra keyword to the language. More annoying to me is that !in doesn't work. --bb
Oct 02 2007
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Bill Baxter wrote:

 More annoying to me is that !in doesn't work.
What's the logical negation of a pointer, anyway ? But !in usually ranks high on the dwishlist thing. --anders
Oct 02 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Anders F Björklund wrote:
 Bill Baxter wrote:
 
 More annoying to me is that !in doesn't work.
What's the logical negation of a pointer, anyway ?
bool negated_pointer = !some_ptr; Seems pretty clear to me. --bb
Oct 04 2007
parent "Janice Caron" <caron800 googlemail.com> writes:
On 10/4/07, Bill Baxter <dnewsgroup billbaxter.com> wrote:
 Anders F Björklund wrote:
 Bill Baxter wrote:

 More annoying to me is that !in doesn't work.
What's the logical negation of a pointer, anyway ?
bool negated_pointer = !some_ptr; Seems pretty clear to me.
Technically speaking, what you've done there is implicitly cast your pointer to bool and _then_ logically negated it. Mathematically speaking, if -(-x) does not equal x then you can't call it a negation. It has to be self-inverse. Logical not has this property when applied to bools !(!x)== x. I think this is the point that Anders was making, with some good humor thrown in. You cannot take the logical not of a pointer. You have to cast it to bool first. Of course this is no problem. I'm just being mentioning this for the hell of it :-)
Oct 04 2007
prev sibling parent "Kyle G." <kyle.james.gibson gmail.com> writes:
Bill Baxter wrote:
 Kyle G. wrote:
 Hi,

 First time posting here on the NG, and certainly not my last.

 D has a good deal of syntactic sugar which results in easier to read 
 code. However, there's one particular thing that has been bothering me 
 for a while now:

 void foo(void[] var) {
  if (var !is null)
   // line of code
 }

 int main() {
  foo(null);
 }

 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not 
 null." Is there any special reason why we are unable to do "var isnot 
 null" or "var is not null"?
Only reason is that it would require adding an extra keyword to the language. More annoying to me is that !in doesn't work. --bb
Doing !(key in map) definitely causes me distress.
Oct 02 2007
prev sibling next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Kyle G. wrote:

 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
A few hundred, search the newsgroup for more details... If you hate it a lot, then just use "if (var)" instead. --anders
Oct 02 2007
parent "Kyle G." <kyle.james.gibson gmail.com> writes:
Anders F Björklund wrote:
 Kyle G. wrote:
 
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not 
 null." Is there any special reason why we are unable to do "var isnot 
 null" or "var is not null"?
A few hundred, search the newsgroup for more details... If you hate it a lot, then just use "if (var)" instead. --anders
It's not that I hate it. In fact, I use !is all the time where it applies. It just looks bad.
Oct 02 2007
prev sibling next sibling parent reply 0ffh <spam frankhirsch.net> writes:
Kyle G. wrote:
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
I think the syntax is perfectly logical, because the negation applies to the /operator/ and not the /operand/.Note the analogy of the statements: (p == null) <--> (p is null) (p != null) <--> (p !is null) Nobody would ever get try to write (i == !null), so why (i is not null)? Regards, Frank
Oct 02 2007
parent reply Gregor Richards <Richards codu.org> writes:
0ffh wrote:
 Kyle G. wrote:
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not 
 null." Is there any special reason why we are unable to do "var isnot 
 null" or "var is not null"?
I think the syntax is perfectly logical, because the negation applies to the /operator/ and not the /operand/.Note the analogy of the statements: (p == null) <--> (p is null) (p != null) <--> (p !is null) Nobody would ever get try to write (i == !null), so why (i is not null)? Regards, Frank
Yeah, to me "a is not null" makes sense as an English sentence, but as an operator it seems like "a is (not null)". (not null) would have to evaluate to ... 1? So this would be a is 1, which isn't what we want at all. Suffice to say D !is English. - Gregor Richards
Oct 02 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Gregor Richards wrote:
 0ffh wrote:
 
 Kyle G. wrote:

 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not 
 null." Is there any special reason why we are unable to do "var isnot 
 null" or "var is not null"?
I think the syntax is perfectly logical, because the negation applies to the /operator/ and not the /operand/.Note the analogy of the statements: (p == null) <--> (p is null) (p != null) <--> (p !is null) Nobody would ever get try to write (i == !null), so why (i is not null)? Regards, Frank
Yeah, to me "a is not null" makes sense as an English sentence, but as an operator it seems like "a is (not null)". (not null) would have to evaluate to ... 1? So this would be a is 1, which isn't what we want at all. Suffice to say D !is English. - Gregor Richards
I'd like to point out that "is not" is exactly what the Python equivalent of "!is" is. http://docs.python.org/ref/comparisons.html if a is not None: foo() Likewise, it also uses "not in". -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Oct 02 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Kirk McDonald wrote:
 Gregor Richards wrote:
 0ffh wrote:

 Kyle G. wrote:

 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not 
 null." Is there any special reason why we are unable to do "var 
 isnot null" or "var is not null"?
I think the syntax is perfectly logical, because the negation applies to the /operator/ and not the /operand/.Note the analogy of the statements: (p == null) <--> (p is null) (p != null) <--> (p !is null) Nobody would ever get try to write (i == !null), so why (i is not null)? Regards, Frank
Yeah, to me "a is not null" makes sense as an English sentence, but as an operator it seems like "a is (not null)". (not null) would have to evaluate to ... 1? So this would be a is 1, which isn't what we want at all. Suffice to say D !is English. - Gregor Richards
I'd like to point out that "is not" is exactly what the Python equivalent of "!is" is. http://docs.python.org/ref/comparisons.html if a is not None: foo() Likewise, it also uses "not in".
And also the binary boolean operators 'and' and 'or'. if you is not None and I is not finished or they is happy: goto_skool() See reads just like English! --bb
Oct 02 2007
parent Lutger <lutger.blijdestijn gmail.com> writes:
Bill Baxter wrote:
    if you is not None and I is not finished or they is happy:
         goto_skool()
 
 See reads just like English!
 
 --bb
It took me a moment to realize this can be valid code. Perhaps it is the lack of syntax highlighting, but as code, it is hard to read for me.
Oct 03 2007
prev sibling parent 0ffh <spam frankhirsch.net> writes:
Kirk McDonald wrote:
 I'd like to point out that "is not" is exactly what the Python 
 equivalent of "!is" is.
 [...]
 Likewise, it also uses "not in".
Well, "not in" seems okay but "is not" looks.... like wierd! Let "not" be a prefix, and a prefix always, lest you flummox the coder! :) Regards, Frank
Oct 02 2007
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 02 Oct 2007 17:23:48 -0400, Kyle G. wrote:

 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
This gets discussed every 6-months or so ... my favourite replacement keyword so far is "aint" as in "if (var aint null) " :-) -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Oct 02 2007
next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 On Tue, 02 Oct 2007 17:23:48 -0400, Kyle G. wrote:
 
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
This gets discussed every 6-months or so ... my favourite replacement keyword so far is "aint" as in "if (var aint null) " :-)
Which reminds me, I need to petition for Ruby to have 'is'/'aint' added to it. They've already got 'unless' and 'until', so why not? -- Chris Nicholson-Sauls
Oct 02 2007
parent reply Robert Fraser <fraserofhenight gmail.com> writes:
Chris Nicholson-Sauls Wrote:

 Derek Parnell wrote:
 On Tue, 02 Oct 2007 17:23:48 -0400, Kyle G. wrote:
 
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
This gets discussed every 6-months or so ... my favourite replacement keyword so far is "aint" as in "if (var aint null) " :-)
Which reminds me, I need to petition for Ruby to have 'is'/'aint' added to it. They've already got 'unless' and 'until', so why not? -- Chris Nicholson-Sauls
From some experience in Perl, I can say unless(P) is slightly clearer than if(!P) and until(P) is slightly clearer than while(!P) ... in most cases. I'm not arguing it should be added to D (esp. since if there are multiple ways to express something, stuff can get confusing), but they _do_ make code just that much easier to read. On that note, I'm also a slight fan of the postfix notation for simple ifs (i.e. something like "a = b if b;" or "file.read() while file.hasMoreBytes();"), but it makes parsing a *****.
Oct 03 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Robert Fraser" <fraserofhenight gmail.com> wrote in message 
news:fdvg0m$2c3e$1 digitalmars.com...
 From some experience in Perl, I can say unless(P) is slightly clearer than 
 if(!P) and until(P) is slightly clearer than while(!P) ... in most cases. 
 I'm not arguing it should be added to D (esp. since if there are multiple 
 ways to express something, stuff can get confusing), but they _do_ make 
 code just that much easier to read.

 On that note, I'm also a slight fan of the postfix notation for simple ifs 
 (i.e. something like "a = b if b;" or "file.read() while 
 file.hasMoreBytes();"), but it makes parsing a *****.
Course you also get people who think it's "cute" to do something like while(someCondition) { ..... 3 pages of code... ..... } if(someOtherCondition)
Oct 03 2007
prev sibling parent reply Alexander Panek <a.panek brainsware.org> writes:
Derek Parnell wrote:
 This gets discussed every 6-months or so ... my favourite replacement
 keyword so far is "aint" as in "if (var aint null) "   :-)
I vote for if (var uint null) !
Oct 03 2007
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Alexander Panek wrote:

 This gets discussed every 6-months or so ... my favourite replacement
 keyword so far is "aint" as in "if (var aint null) "   :-)
I vote for if (var uint null) !
I thought the "if (var out null)" was somewhat amusing too... --anders
Oct 03 2007
prev sibling next sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Kyle G. wrote:
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
It all depends on what the meaning of "is" is.
Oct 02 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Walter Bright wrote:
 Kyle G. wrote:
 Is there any special reason why we are unable to do "var isnot null" 
 or "var is not null"?
It all depends on what the meaning of "is" is.
That's a good quote to go with your collection of famous things nobody ever said about D. You can attribute it to Dill Dinton. --bb, hoping you don't take this suggestion seriously.
Oct 02 2007
prev sibling parent "Kyle G." <kyle.james.gibson gmail.com> writes:
Walter Bright wrote:
 Kyle G. wrote:
 Is there any special reason why we are unable to do "var isnot null" 
 or "var is not null"?
It all depends on what the meaning of "is" is.
That sounds like quote from a US politician. And so without 'is,' ... In this context, the first 'is' attempts to discern whether their exists a known accepted justification for the usage of "!is" as opposed to something potentially cleaner and easier to read. It appears the justification which has been applied here follows that an extra keyword in the language would not be necessary when existing language keywords suffice.
Oct 02 2007
prev sibling parent Robert Fraser <fraserofhenight gmail.com> writes:
Derek Parnell Wrote:

 On Tue, 02 Oct 2007 17:23:48 -0400, Kyle G. wrote:
 
 The code I am concerned about is "var !is null" which appears to 
 translate to "var not is null" when it actually means "var is not null." 
 Is there any special reason why we are unable to do "var isnot null" or 
 "var is not null"?
This gets discussed every 6-months or so ... my favourite replacement keyword so far is "aint" as in "if (var aint null) " :-) -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Now _that's_ the sort of syntax feature we need to make D a major and respected player in the software engineering world.
Oct 03 2007