A thought for template alias parameters?

Trip Volpe mraccident at gmail.com
Sun Feb 14 20:03:30 PST 2010

Lutger Wrote:
> This syntax should be possible, which also gives syntax highlighting:
> expectEquals!q{ myFoo == 3 };
> But there is the problem that expectEquals is defined in a module which 
> doesn't have access to myFoo. I don't know how to solve that.

Template alias parameters handle it nicely right now, at least in the case where you only need to use a single symbol and not a compound expression:

In mycontracts.d:

void expectEqual(alias A, alias B)
        ( string file = __FILE__, int line = __LINE__ ) {
        if(A != B) {
                string msg = format("expected: %s==%s; actual: %s, %s",
			A.stringof, B.stringof, A, B );
                fail( msg, file, line );

In somethingelse.d:

    expectEqual! ( myFoo, 3 );

Even where expectEqual is defined in a different module, the symbol myFoo is still evaluated in the correct context (the context in which expectEqual is being instantiated).

What I'm suggesting is that alias parameters be expanded to allow compound expressions. That would allow you to also use object fields, array elements, and so forth in expectEqual, like so:

    expectEqual! ( foo.baz[2], 42 );
    expectEqual! ( wibble(), "OK" );

You could also do expect with a single expression and no awkward boilerplate:

    expect! ( myFoo == 3 );

I think this would be a great enhancement; I was just curious if there's any good reason _not_ to do this that I'm not aware of.  :-)

More information about the Digitalmars-d mailing list