www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Another use-case for multiple alias this

I've noticed something that's happening in many D libraries, some data
structures are constantly being reinvented. The classic example is the
Rectangle struct.

For example, CairoD defines a Rect struct, but so do other libraries
like DGui, Qtd, and practically any library which has to deal with
drawing something on the screen.

The problem when using multiple libraries, is that you end up having a
hard time interacting with these libraries when you have to take care
which exact Rect structure you can pass to some library function, for

// library code
void fooLibDrawRect(Rect rect);  // actually takes foo.rect
void barLibDrawRect(Rect rect);  // actually takes bar.rect

// user code
import foo;
import bar;
void main()
    fooLibDrawRect(Rect(...));  // usually a symbol error if there's 2
different Rect definitions

So the current fix for this is to use either explicit symbol lookup or
to introduce aliases:

import foo;
import bar;
alias FooRect = foo.Rect;
alias BarRect = foo.Rect;

void main() {

If we had multiple alias this support, then the user could introduce
his own Rect struct that wraps all the other Rect structs to make
using the actual libraries much simpler:

struct Rect  //
    int x, y, width, height;

    /* These might do conversions if necessary,
        or they could use a static cast of the "this" object */
     property auto getFooRect() { return foo.Rect(...); }
     property auto getBarRect() { return bar.Rect(...); }

    alias getFooRect this;
    alias getBarRect this;

void main() {
    fooLibDrawRect(Rect(...));  // works
    barLibDrawRect(Rect(...));  // also works

I think this would be a major selling point for D.

And in fact multiple alias this is a feature we've practically all
agreed on, but what's holding it from being finally implemented?
Aug 05 2013