May 31, 2018 Re: string file = __FILE__ considered harmful (and solution) | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 5/30/2018 2:45 PM, John Colvin wrote:
> https://run.dlang.io/is/oMe7KQ
>
> Less elegant, but solves the problem of accidental argument adding (CallerFile acts as a barrier). Unfortunately, while it works in theory, in practice the compiler crashes.... LOL
Please post bug reports when you find compiler crashes. Thanks!
|
May 31, 2018 Re: string file = __FILE__ considered harmful (and solution) | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | On 5/30/2018 1:27 AM, FeepingCreature wrote:
> There's a very common idiom where in order to report line numbers of an error or a log line at the callsite of a function, you pass __FILE__ and __LINE__ as default parameters:
>
> void foo(string file = __FILE__, size_t line = __LINE__);
>
> What's wrong with this?
>
> Say you add a string parameter, such as
>
> void foo(string msg, string file = __FILE__, size_t line = __LINE__);
>
> foo("Hello World");
>
> Now when you accidentally grab an old version of the library, your new code will still run, but it will believe that it's being called from file "Hello World", line 15.
A solution:
enum E { reserved }
void foo(E e = E.reserved, string file = __FILE__, size_t line = __LINE__);
void foo(string msg, E e = E.reserved, string file = __FILE__, size_t line = __LINE__);
|
June 01, 2018 Re: string file = __FILE__ considered harmful (and solution) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Friday, 1 June 2018 at 03:14:11 UTC, Walter Bright wrote: > On 5/30/2018 2:45 PM, John Colvin wrote: >> https://run.dlang.io/is/oMe7KQ >> >> Less elegant, but solves the problem of accidental argument adding (CallerFile acts as a barrier). Unfortunately, while it works in theory, in practice the compiler crashes.... LOL > > Please post bug reports when you find compiler crashes. Thanks! It's the same issue as this (https://issues.dlang.org/show_bug.cgi?id=18916) only for __FILE__. I already have a pending PR at https://github.com/dlang/dmd/pull/8310 which I can modify to fix for both __LINE__ and __FILE_ if I can get clarification on how it *should* work. Mike |
June 01, 2018 Re: string file = __FILE__ considered harmful (and solution) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Franklin | On 6/1/2018 12:30 AM, Mike Franklin wrote:
> I already have a pending PR at https://github.com/dlang/dmd/pull/8310 which I can modify to fix for both __LINE__ and __FILE_ if I can get clarification on how it *should* work.
I followed up there. Thanks for taking care of this!
|
Copyright © 1999-2021 by the D Language Foundation