www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 'in' keyword, anyone?

reply Sam <Sam_member pathlink.com> writes:
I need an 'in' keyword that behaves like the following:

bool b = a in {"AAAA", "BB", "CCCCCC", "Lemon"};

-or-

bool b = a in CollectionObject;
b = a in ArrayObject;

Where the above statement is the same as:

bool b;

switch(a)
{
case "AAAA":
b = 1 // true!!
break;

case "BB":
b = 1;
break;

case "CCCCCC":
b = 1;
break;

case "Lemon":
b = 1;
break;

case else:
b = 0; // false!!  I refuse to use keywords imposed by the MAN!
break;
}

Maybe I use too much SQL, but 'in' used in this fashion would be nice!

See?  I'm not against ALL keywords!!  (-;
May 26 2005
next sibling parent Brad Beveridge <brad somewhere.net> writes:
Sam wrote:
 I need an 'in' keyword that behaves like the following:

Brad
May 26 2005
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 26 May 2005 18:52:52 +0000 (UTC), Sam wrote:

 I need an 'in' keyword that behaves like the following:
 
 bool b = a in {"AAAA", "BB", "CCCCCC", "Lemon"};

int[char[]] theList; theList["AAAA"] = 1; theList["BB"] = 2; theList["CCCCCC"] = 3; theList["Lemon"] = 4; bool b = a in theList ? true : false; -- Derek Parnell Melbourne, Australia 27/05/2005 6:10:49 AM
May 26 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 bool b = a in theList ? true : false;

Or even just this: # bool b = (a in theList) == null; Except he says he doesn't like the 'null' keyword. And I wonder, would a null pointer implicity cast to a 'false' and a valid pointer implicitly cast to a 'true'? Then you could just do: # bool b = a in theList; -- Chris Sauls
May 26 2005
parent Derek Parnell <derek psych.ward> writes:
On Thu, 26 May 2005 21:13:41 -0500, Chris Sauls wrote:

 Derek Parnell wrote:
 bool b = a in theList ? true : false;


I'm quite pedantic with my bools and I don't like to implicitly convert integers to bools. Just a thing I have... ;-)
 Or even just this:
 
 # bool b = (a in theList) == null;

Or more like this ... bool b = (a in theList) != null;
 Except he says he doesn't like the 'null' keyword.  And I wonder, would 
 a null pointer implicity cast to a 'false' and a valid pointer 
 implicitly cast to a 'true'?  Then you could just do:
 
 # bool b = a in theList;

Nup, D is smart enough to realize that pointers are not integers so an implicit conversion doesn't fly. Though this (ugly construct) is possible ... bool b = !!(a in theList); -- Derek Melbourne, Australia 27/05/2005 12:22:31 PM
May 26 2005
prev sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Sam wrote:
 I need an 'in' keyword that behaves like the following:
 
 bool b = a in {"AAAA", "BB", "CCCCCC", "Lemon"};
 
 -or-
 
 bool b = a in CollectionObject;
 b = a in ArrayObject;
 
 Where the above statement is the same as:
 
 bool b;
 
 switch(a)
 {
 case "AAAA":
 b = 1 // true!!
 break;
 
 case "BB":
 b = 1;
 break;
 
 case "CCCCCC":
 b = 1;
 break;
 
 case "Lemon":
 b = 1;
 break;
 
 case else:
 b = 0; // false!!  I refuse to use keywords imposed by the MAN!
 break;
 }
 
 Maybe I use too much SQL, but 'in' used in this fashion would be nice!
 
 See?  I'm not against ALL keywords!!  (-;
 
 

There IS an "in" keyword ... But I just don't know how to use it.
May 26 2005