hidden passing of __FILE__ and __LINE__ into function

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 17 03:30:35 PDT 2017


On Monday, 17 April 2017 at 10:22:47 UTC, Dmitry wrote:
> Hi there.
>
> Currently for messages about errors I use code like this:
>
>     void add(string name, ref Scene scene)
>     {
>         if (name in listOfScenes)
>         {
>             EError(__FILE__, __LINE__, "scene already 
> exists".L, quoted(name));
>         }
>     ...
>     }
>
> Is there way for avoid using (avoid writing) `__FILE__` and 
> `__LINE__` in each call? I.e. I want use simple
>
>     EError("scene already exists".L, quoted(name));
>
> but function `EError` must print info (file and line) of where 
> was called.
>
> P.S. `EError` just prints info into console, result for this 
> example is:
>
>> [Error] (source\core\EScene.d, 35) Scene already exists: 
>> "Scene 1"
>
> and code is:
>
>     void EError(S...)(S args)
>     {
>         write("[Error] (", args[0], ", ", args[1], ") ", 
> args[2..$], '\n');}
>     }

when used as template value parameter, __FILE__ and 
__LINE__evaluate to the file and line of the call site. So help 
yourself ans use something like this:

void error(string f = __FILE__, int l = __LINE__)(string msg){}


More information about the Digitalmars-d-learn mailing list