Skip to content

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

@scottbilas

Description

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: #192 (comment))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area: ioIssues related to core terminal I/O.state: approvedEnhancements and tasks that have been approved.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions