More D newb questions.

Walter Bright newshound1 at digitalmars.com
Tue May 6 10:43:41 PDT 2008


Steven Schveighoffer wrote:
> This is the only sound reasoning that you have, but it's not that adding the 
> concat operator for basic types would break things, it's that adding it 
> would be a redundant method of doing [T, T], which makes sense to me.

It breaks things because concatenating two arrays is a very, very 
different thing than forming a new array with two values in it. The 
ambiguity comes when the value is an array type:

typeof(t1~t2) makePair(T t1, T t2)
{
     return t1 ~ t2;
}

int[] a;
char c;

makePair(c,c);   // ok, I get an array with two elements, what I want
makePair(a,a);   // I don't get a pair of arrays at all, big surprise

Now, let's consider the struct case. I overload opCat for my containter 
struct. How do I distinguish wanting a *pair* of those structs from 
wanting to concatenate the *contents* of the container?

Right now, it's very simple. To make a pair:

      return [t1, t2];

To concatenate the contents:

      return t1 ~ t2;

There is no ambiguity of intent, and if our struct doesn't support 
concatenation of its contents, you get a nice compiler error.

In other words, opCat for a struct means "concatenate the container 
contents", not concatenate the struct value.



More information about the Digitalmars-d mailing list