summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-12-08 10:56:59 +0100
committerEugen Wissner <belka@caraus.de>2017-12-08 10:56:59 +0100
commit7fe69ccc5c4418f23f960d25d3b6e2f6ce775345 (patch)
treefd458bfb5c7e4ac6835a2c0a8f6ad9d512bcd007
parent26c3532e282f549f5634f131f042c4c13349e69b (diff)
downloadtanya-7fe69ccc5c4418f23f960d25d3b6e2f6ce775345.tar.gz
format: Aggregate types
-rw-r--r--source/tanya/format/package.d49
1 files changed, 48 insertions, 1 deletions
diff --git a/source/tanya/format/package.d b/source/tanya/format/package.d
index e283c3d..2c19ada 100644
--- a/source/tanya/format/package.d
+++ b/source/tanya/format/package.d
@@ -516,6 +516,22 @@ private ref String printToString(string fmt, Args...)(return ref String result,
{
result.insertBack(args[0] ? "true" : "false");
}
+ else static if (is(Unqual!(typeof(args[0].stringify())) == String))
+ {
+ result.insertBack(args[0].stringify()[]);
+ }
+ else static if (isClass!(Args[0]))
+ {
+ result.insertBack(args[0].toString());
+ }
+ else static if (isInterface!(Args[0]))
+ {
+ result.insertBack(Args[0].classinfo.name);
+ }
+ else static if (isStruct!(Args[0]))
+ {
+ result.insertBack(typeid(Args[0]).name);
+ }
else static if (isSomeString!(Args[0])) // String
{
if (args[0] is null)
@@ -750,7 +766,7 @@ private ref String printToString(string fmt, Args...)(return ref String result,
}
else
{
- static assert(false);
+ result.insertBack(Args[0].stringof);
}
ParamEnd:
@@ -831,6 +847,37 @@ package(tanya) String format(string fmt, Args...)(auto ref Args args)
assert(format!"{}"(cast(void*) null) == "0x0");
}
+// Aggregate types.
+@system unittest // Object.toString has no attributes.
+{
+ import tanya.memory;
+ import tanya.memory.smartref;
+
+ static struct WithoutStringify
+ {
+ }
+ assert(format!"{}"(WithoutStringify()) == typeid(WithoutStringify).name);
+
+ static struct WithStringify
+ {
+ String stringify() const @nogc nothrow pure @safe
+ {
+ return String("stringify method");
+ }
+ }
+ assert(format!"{}"(WithStringify()) == "stringify method");
+
+ interface I
+ {
+ }
+ class A : I
+ {
+ }
+ auto instance = defaultAllocator.unique!A();
+ assert(format!"{}"(instance.get()) == instance.get.toString());
+ assert(format!"{}"(cast(I) instance.get()) == I.classinfo.name);
+}
+
private struct FormatSpec
{
}