On Sunday, 15 February 2015 at 17:18:08 UTC, Steve D wrote:
Python (built-in)
dict1 = {"a":1,"b":2}
tup1 = (0,1,2,3)
arr1 = [0,1,2,3] # list type
str1 = "abcd"
print "b" in dict1 # True
print 3 in tup1 # True
print 3 in arr1 # True
print "c" in str1 # True
print tup1.index(2) # 2
print arr1.index(2) # 2
print str1.index("c") # 2
There is some sort of consistency of use, though they are
different sequence/collection types.
Now try the same in D
auto dict1 = ["a":1,"b":2];
auto tup1 = tuple(0,1,2,3);
auto arr1 = [0,1,2,3];
auto str1 = "abcd";
Having some consistency involving
sequences/arrays/strings/collections etc, which are the
foundations of any language makes programming much easier,
intuitive and pleasant. It shouldn't be too difficult for a
super bare-metal language like D.
I'm honestly not knocking D (I love it), just some thoughts
(maybe provoke some discussion?, for D3?)
Part of the issue is that "in" means to search by key, not by
value. In that respect, it only makes sense for dictionaries (and
perhaps sets). I guess the idea is that this generic code should
be valid for any container which implements the in operator:
if(a in r) writeln(r[a]);
On a different note, I do wish D had tuple literals like Python
has.