Get current line of source file in D

Is there a way to get the current line in the source file you are on, for example __LINE__in C ++?

+5
source share
1 answer

Yes you can use __LINE__. In addition, __FILE__.

See Keywords section.

As noted by BCS and Jonathan M Davis in the comments, __LINE__there is a special case for friends: when using the function as the default value for the template or argument, they allow the location of the caller, not the signature of the template or function. This is great for preventing users from providing 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);
     }
}
+9
source

All Articles