On 11/3/22 03:00, Bruno Pagis wrote:
> void print() {
> writeln("array = ", this.array);
> }
Similar to Paul Backus's program but I would try to avoid the file
system for unit tests when possible. In this case, you can print into a
sink, which can be useful in other ways as well:
import std.stdio;
class A
{
int[] a;
this(int[] a) { this.a = a; }
void toString(scope void delegate(in char[]) sink) const {
import std.conv : text;
sink("array = ");
sink(this.a.text); // or this.a.to!string
// The following alternative may be a little heavier weight:
// import std.format : formattedWrite;
// sink.formattedWrite!"array = %s"(this.a);
}
void print() {
writeln(this);
}
}
unittest {
import std.file;
A a = new A([1, 2]);
import std.conv : text;
assert(a.text == "array = [1, 2]");
}
void main() {}
toString is the textual representation of the object. If you don't want
the same output for that purpose, you can use a different name from
toString but you have to use an explicit "sink" but I really think the
above is what you want. :)
Ali