On 2018-07-31 09:17, Shachar Shemesh wrote:
I'm trying to figure out what's the signature of the built-in assert. It
does not seem that I can define a similar function myself.
First attempt:
void myAssert(bool cond, string msg) @nogc nothrow;
No, because msg gets evaluated unconditionally.
void myAssert(bool cond, lazy string msg) @nogc nothrow;
test.d(8): Error: @nogc function test.myAssert cannot call non-@nogc
delegate msg
void myAssert(bool cond, lazy string msg @nogc nothrow ) @nogc nothrow;
test.d(4): Error: found @ when expecting )
test.d(4): Error: semicolon expected following function declaration
test.d(4): Error: no identifier for declarator nogc
test.d(4): Error: declaration expected, not )
test.d(9): Error: unrecognized declaration
Templates to the rescue!!!
void myAssert(STR)(bool cond, lazy STR msg );
test.d(14): Error: @nogc function D main cannot call non-@nogc function
test.myAssert!string.myAssert
It seems this is a limitation in the syntax. It works with an explicit
delegate, but then that is required at the call site as well. A lazy
parameter is basically just a delegate with a nicer syntax.
void myAssert(bool cond, string delegate() @nogc nothrow msg) @nogc nothrow
{
auto a = msg();
}
Support for adding UDAs to function parameters was recently added.
Perhaps we need to support other attributes as well.
Please report an issue to http://issues.dlang.org.
--
/Jacob Carlborg