www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: A thought for template alias parameters?

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. :-)
Feb 14 2010