On Friday, 21 May 2021 at 16:53:48 UTC, drug wrote:
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.

Ahh, in that case it would appear formattedWrite isn't @safe at all. Looks like you have to stick with put()?
```d
@safe void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
        //writer.formattedWrite!("FOO:%s", x); // fails
        import std.conv;
        put(writer, "FOO:");
        put(writer, x.to!string);
}
```

Reply via email to