Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current line of source file in D

Tags:

d

Is there a way to get the current line in the source file you are on, like __LINE__ does in C++?

like image 572
John Zane Avatar asked Dec 15 '10 22:12

John Zane


People also ask

How do you get the line number in a file in Python?

Use readlines() to get Line Count This is the most straightforward way to count the number of lines in a text file in Python. The readlines() method reads all lines from a file and stores it in a list. Next, use the len() function to find the length of the list which is nothing but total lines present in a file.

How do you print a line of code in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do I get the line number in C++?

The standard solution to find the current line number in C++ is using the predefined macro __LINE__ . It returns the integer value representing the current line in the source code file being compiled. The preprocessor will simply replace the __LINE__ macro with the current line number.

How do I print line numbers in printf?

LINE The presumed line number (within the current source file) of the current source line (an integer constant). As an integer constant, code can often assume the value is __LINE__ <= INT_MAX and so the type is int . To print in C, printf() needs the matching specifier: "%d" .


1 Answers

Yep, you can use __LINE__. Also, __FILE__.

See Keywords section

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);
     }
}
like image 63
Justin W Avatar answered Sep 24 '22 15:09

Justin W