interface と Object

なんのことはない。
interface は Object に(明示的に) cast できた。

2006-3-20 に書いた interface は Object として扱えないという問題も cast すれば殆ど問題にならない。

それにともない、もう変えないと言っていた文字列化関数を更新した。
interface の事前条件がなくなり、interface は無条件で文字列化可能になった。

template ToString(T) {
  char[] ToString(T value)
    in {
      static assert (!(
         is (T == function)  &&
        !is (T == char[](*)())
      ));
      static assert (!(
         is (T == delegate)  &&
        !is (T == char[] delegate())
      ));
      static assert (!(
         is (T == union)     &&
        !is (typeof(T.toString))
      ));
      static assert (!(
         is (T == struct)    &&
        !is (typeof(T.toString))
      ));
    }
    body {
      static    if (is (T : char[]))
        return value;

      else static if (is (T == char[](*)()))
        return value();

      else static if (is (T == char[] delegate()))
        return value();

      else static if (is (T == interface))
        return (cast(Object)value).toString();

      else static if (is (
        typeof(T.toString) == typeof(Object.toString)
      ))
        return value.toString();

      else static if (is (typeof(T.init.keys)))
        return MapToString!(
          typeof(T.init.keys[0]),
          typeof(T.init.values[0])
        )(value);

      else static if (is (typeof(T.init)[(T).length]))  
        return ArrayToString!(
          typeof(*T.ptr),
          (T).length
        )(value);

      else static if (is (typeof(T.init.ptr)))
        return ArrayToString!(typeof(*T.ptr))(value);

      else
        return std.string.toString(value);
    }
}