Reputation: 888
Is there a way to get the current line in the source file you are on, like __LINE__
does in C++?
Upvotes: 5
Views: 231
Reputation: 2077
Yep, you can use __LINE__
. Also, __FILE__
.
As BCS and Jonathan M Davis point out in the comments, there is a special case for __LINE__
and friends: when used as the default value of a template or function argument, they resolve to the location of the caller, not the signature of the template or function. This is great for saving callers from having to provide this information.
void myAssert(T)(lazy T expression, string file = __FILE__, int line = __LINE__)
{
if (!expression)
{
// Write the caller location
writefln("Assert failure at %s:%s", file, line);
}
}
Upvotes: 9