toString best practices

Paolo Invernizzi paolo.invernizzi at gmail.com
Fri Feb 17 11:26:36 UTC 2023


On Wednesday, 15 February 2023 at 12:15:18 UTC, Bastiaan Veelo 
wrote:
> On Thursday, 9 February 2023 at 17:49:58 UTC, Paolo Invernizzi 
> wrote:
>> ```
>> import std.format, std.range.primitives;
>>
>> struct Point(T)
>> {
>>     T x, y;
>>
>>     void toString(W)(ref W writer, scope const ref 
>> FormatSpec!char f) const
>>     if (isOutputRange!(W, char))
>>     {
>>         put(writer, "(");
>>         formatValue(writer, x, f);
>>         put(writer, ", ");
>>         formatValue(writer, y, f);
>>         put(writer, ")");
>>     }
>> }
>>
>> void main(){
>>
>>         import std.format : format;
>>         assert( format("%s", Point!int(1,2)) == "(1, 2)");
>>
>>     	import std.experimental.logger;
>>     	sharedLog.infof("%s", Point!int(1,2));
>> }
>> ```
>
> Pasting this into https://run.dlang.io/, it just works. That's 
> for DMD 2.099, so it might be a regression -- or recent feature?
>
> -- Bastiaan.

Hi Bastiaan,

I think the cause is in some change happened in the logger 
module, I'm recompiling some code with the latest dmd frontend.

I solved it simply using the 'logX' functions at module level 
instead of the sharedLog methods.

Thank you!


More information about the Digitalmars-d-learn mailing list