cathode icon indicating copy to clipboard operation
cathode copied to clipboard

Out<T> and Print<T> problematic as generic methods

Open scottbilas opened this issue 6 months ago • 12 comments

I've run into problems with the Print<T> and Out<T> generics. These do a ToString() on whatever is passed in, which is generally nice for usability, but I have more than once accidentally passed in something that gets auto-stringized, rather than the compile failing and catching my mistake.

Furthermore, the generic blocks my ability to add extension methods for custom type handling. A great example of this is when I wanted to add an Out(Spectre.Console.Rendering.IRenderable) extension. Unfortunately, the Out<T> always wins over an extension, and for any parameter type. And if I simply forget to call AnsiConsole.Console.ToAnsi first, then I get the default/bad Object.ToString behavior. IMO these generic functions cause more problems than they solve.

I can think of a few potential improvements:

  • Remove Out<T> entirely
  • Rename to OutValue<T>
  • Move Out<T> to an extension method
  • Change it to Out(object) (as with System.IO.TextWriter, adding in a bunch of overloads for primitives to reduce boxing)

My favorite is the first option. Cathode feels to me much more close-to-the-metal API vs the console stuff .NET ships with. Lots of work has gone into minimizing allocs and avoiding copying. So these stringizing helper methods feel out of place to me.

(Related: https://github.com/vezel-dev/cathode/issues/192#issuecomment-2293660796)

scottbilas avatar Aug 16 '24 14:08 scottbilas