Very often I use associative array as the simple syntactic way of representing of pairs of some values. But AAs are slightly complicated for this task and they don't preserve order inside it. I could use some struct for example

struct P(T, N)
{
   T first;
   N second;
}

and write something like

auto pairs = [ P(1, "one"), P(2, "two"), P(3, "three") ];

But this looking not very pleasant and it's not obvious what P is, but writing words Pair is verbose. So what I was thinking about it could we have some syntactic sugar for this simple type of data. In my experience of using D I needed it very often and emulated it with AAs. Problem is that I don't always need all the features of it (I meen searching by hash). But syntax like

1. auto pairs = [ 1: "one", 2: "two", 3: "three" ];
2. auto pairs = [ 1 => "one", 2 => "two", 3 => "three" ];
or
3. auto pairs = [ 1 -> "one", 2 -> "two", 3 -> "three" ];
4. something else

It could be useful in my opinion. Resulting type for this could be a simple struct with 'first' and 'second' properties and index operators [0], [1].

Also this could be expanded to 'threes' of values and etc
auto threes = [ 1 -> "one" -> "I", 2 -> "two" -> "II", 3 -> "three" -> "III" ];

In process of writing of this letter I remembered about tuple literal proposal. It can also help to solve this problem, because we can make it not verbose and also it will propose standard struct name for it. For example it could look like this:

auto pairs = [ t{1, "one"} , t{2, "two"}, t{3, "three"} ] ;
auto threes = [ t{1, "one", "I"} , t{2, "two", "II"}, t{3, "three", "III"} ] ;

In this code I assume that t{} is tuple literal. But all these bracers are not that elegant as single -> or => or another operator.

I'd like to see any comments

Reply via email to