We need something like source_location in D

guy hebert guyhebert143 at outlook.com
Tue Sep 24 10:36:53 UTC 2019


On Monday, 5 August 2019 at 14:42:18 UTC, Steven Schveighoffer 
wrote:
> On 7/30/19 9:39 PM, Andrej Mitrovic wrote:
>>  From C++20: 
>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1208r5.pdf
>> 
>> Instead of having to constantly declare "file = __FILE__, line 
>> = __LINE__" everywhere, it would be so much more useful to 
>> just have a single built-in type which we can default 
>> initialize . For example:
>> 
>> -----
>> import std.algorithm;
>> 
>> void checkAndThrow (lazy bool expression, Loc loc = 
>> __LOCATION__)
>> {
>>      if (!expression)
>>          throw new Exception("Failed", loc.file, loc.line);
>> }
>> 
>> void main ()
>> {
>>      auto arr = [1, 1, 1, 1];
>>      checkAndThrow(arr.sum == 4);
>> 
>>      // should throw with file+line of this statement
>>      checkAndThrow(arr.sum == 5);
>> }
>> -----
>
> This was my suggestion a while back:
> https://issues.dlang.org/show_bug.cgi?id=18919
>
> The biggest problem I see with using string and size_t is that 
> those are common parameter types that can be confused with 
> actual parameters. Having a Location type solves that.
> http://www.crunchytricks.com/2016/07/offline-java-compilers.html
> -Steve

Have you got the solution.


More information about the Digitalmars-d mailing list