using System; using System.Linq; using System.Collections.Generic; using System.Collections.Immutable; public interface IString { string Str(); } public static class ToStringImplementations { // These allow string conversion for uncooperative classes // as long as their type arguments are cooperative. // For some reason Str takes precedence over this… public static string Str(this ImmutableList l) where T : IString => $"ImmutableList({l.Select(x => x.Str()).JoinWith(", ")})"; // …but not over this: public static string Str(this ImmutableList l) => $"ImmutableList({l.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this ImmutableList l) => $"ImmutableList({l.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this ImmutableHashSet h) => $"ImmutableHashSet({h.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this IEnumerable e) => $"IEnumerabl({e.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this IEnumerable e) => $"IEnumerab({e.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this IEnumerable e) => $"IEnumerab({e.Select(x => x.Str()).JoinWith(", ")})"; public static string Str(this ImmutableDictionary h) => $"ImmutableDictionary(\n{h.Select(x => $" {x.Key.Str()}:{x.Value.Str()}").JoinWith(",\n")}\n)"; public static string Str(this string s) => $"\"{s}\""; public static string Str(this object o) => ""+o.ToString(); }