www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to bypass the file and line into D assert function ?

reply Newbie2019 <newbie2019 gmail.com> writes:
for example:

void ASSERT(string fmt, string file = __FILE_FULL_PATH__, size_t 
line = __LINE__, T...) (bool c, scope T a)   nogc {
    assert(c, string, file, line);
}

but i get this error:

error.d(39): Error: found file when expecting )
error.d(39): Error: found ) when expecting ; following statement
error.d(39): Deprecation: use { } for an empty statement, not ;

I want d to print the error message with some format information, 
and show the right file and line for the original location.

Is it doable ?
Jul 19 2019
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2019-07-19 17:30, Newbie2019 wrote:
 for example:
 
 void ASSERT(string fmt, string file = __FILE_FULL_PATH__, size_t line = 
 __LINE__, T...) (bool c, scope T a)   nogc {
     assert(c, string, file, line);
 }
 
 but i get this error:
 
 error.d(39): Error: found file when expecting )
 error.d(39): Error: found ) when expecting ; following statement
 error.d(39): Deprecation: use { } for an empty statement, not ;
 
 I want d to print the error message with some format information, and 
 show the right file and line for the original location.
 
 Is it doable ?
No, not as far as I know. But you can throw an `AssertError` explicitly, which is what `assert` does when it fails. This allows to pass the file and line information [1]. Or call `onAssertError` which if called first, when an `assert` fails [2]. [1] https://github.com/dlang/druntime/blob/3620736bc88129b65f9a01290189de617a6c3b07/src/core/exception.d#L81-L84 [2] https://github.com/dlang/druntime/blob/3620736bc88129b65f9a01290189de617a6c3b07/src/core/exception.d#L417-L449 -- /Jacob Carlborg
Jul 19 2019
prev sibling parent reply Max Haughton <maxhaton gmail.com> writes:
On Friday, 19 July 2019 at 15:30:25 UTC, Newbie2019 wrote:
 for example:

 void ASSERT(string fmt, string file = __FILE_FULL_PATH__, 
 size_t line = __LINE__, T...) (bool c, scope T a)   nogc {
    assert(c, string, file, line);
 }

 but i get this error:

 error.d(39): Error: found file when expecting )
 error.d(39): Error: found ) when expecting ; following statement
 error.d(39): Deprecation: use { } for an empty statement, not ;

 I want d to print the error message with some format 
 information, and show the right file and line for the original 
 location.

 Is it doable ?
Isn't assert a template (file and line) rather than a plain function call? Worst comes to worst, you can provide your own _d_assert(?) and override object.d then just call the C assert
Jul 19 2019
parent reply Jacob Carlborg <doob me.com> writes:
On 2019-07-19 22:16, Max Haughton wrote:

 Isn't assert a template (file and line) rather than a plain function call?
No. It's a keyword, it's built-in to the compiler. It get extra benefits compared to a regular functions: the asserts will be removed in release builds. -- /Jacob Carlborg
Jul 22 2019
parent Newbie2019 <newbie2019 gmail.com> writes:
On Monday, 22 July 2019 at 09:54:13 UTC, Jacob Carlborg wrote:
 On 2019-07-19 22:16, Max Haughton wrote:

 Isn't assert a template (file and line) rather than a plain 
 function call?
No. It's a keyword, it's built-in to the compiler. It get extra benefits compared to a regular functions: the asserts will be removed in release builds.
Thanks for explain.
Jul 22 2019