More D newb questions.

Janice Caron caron800 at googlemail.com
Fri May 9 21:29:32 PDT 2008


On 09/05/2008, Derek Parnell <derek at psych.ward> wrote:
>      T a, b;
>      T[] foo = a ~~ b;
>      T[] bar = foo ~~ a;
>      T[] qwe = b ~~ foo;
>
>  Is there ANY way that this SORT OF thing could be made to work in D?

For crying out loud, yes!

    T a,b;
    T[] foo = [ a, b ];
    T[] bar = foo ~ a;
    T[] qwe = b ~ foo;

It's not hard.

If you wanted, you could even templatise it:

    T[] join(T:U[],U)(T array, U element) { return array ~ element; }
    T[] join(T:U[],U)(U element, T array) { return element ~ array; }
    T[] join(T:U[],U)(T array1, T array2) { return array1 ~ array2; }
    T[] join(T)(T element1, T element2) { return [ element1, element2 ]; }

    T a,b;
    T[] foo = join(a, b);
    T[] bar = join(foo, a);
    T[] qwe = join(b, foo);

However, that template won't work with custom collections - only with
built in arrays.

Overloading a function is a perfectly normal and expected thing to do in D.
Redefining an operator, on the other hand, is a BIG DEAL, hence the resistance.

As for introducing a new operator, well, what's the point?



More information about the Digitalmars-d mailing list