Output Range Problem? How to make it work?

Paul Backus snarwin at gmail.com
Mon Oct 11 00:37:43 UTC 2021


On Monday, 11 October 2021 at 00:19:44 UTC, apz28 wrote:
> /* Getting this error
> onlineapp.d(34): Error: none of the overloads of `toString` are 
> callable using argument types `(Buffer)`, candidates are:
> onlineapp.d(19):        `onlineapp.Foo.toString()`
> onlineapp.d(26):        `onlineapp.Foo.toString(const(char)[] 
> fmt)`
> onlineapp.d(22):        `toString(Writer, Char)(return ref 
> Writer sink)`
> */

The signature of your `toString` method should match one of the 
examples in [the documentation][1]:

```d
void toString(Writer, Char)(ref Writer w, const ref 
FormatSpec!Char fmt)
void toString(Writer)(ref Writer w)
string toString();
```

Here is an example that works:

```d
import std.range: put;
import std.array: appender;
import std.format: formattedWrite;

struct Foo
{
     void toString(Writer)(ref Writer writer)
     {
         put(writer, "Foo");
     }
}

void main()
{
     auto buffer = appender!string;
     Foo foo;
     formattedWrite(buffer, "%s", foo);
     assert(buffer[] == "Foo");
}
```

Link: https://run.dlang.io/is/dZLRuo


[1]: https://phobos.dpldocs.info/std.format.write.html


More information about the Digitalmars-d-learn mailing list