Format

drug drug2004 at bk.ru
Fri May 21 16:53:48 UTC 2021


21.05.2021 18:28, cc пишет:
> On Friday, 21 May 2021 at 14:19:03 UTC, newbie wrote:
>> Thank you, and formatValue?
> 
> formattedWrite should handle this.
> 
> ```d
> @safe struct Foo {
>      int x = 3;
>      void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
>          writer.formattedWrite("Foo(%s)", x);
>      }
> }
> Foo foo;
> writeln(foo);
> ```
> 
> Oddly enough this form of toString works from @safe code even if it's 
> not marked @safe, or even marked @system...

I guess that because it is a template so the compiler is able to deduce 
its attributes. What about the case when it is marked @system - in this 
case compiler ignore that method due to the fact that it is @system and 
generate the default one. Because your implementation of the method is 
equal to the default implementation you didn't see the difference but it 
exists. Try to make your implementation of `toString` different and 
you'll see.


More information about the Digitalmars-d-learn mailing list