IFormattable with Union-based Formats #133609
Unanswered
TheBrambleShark
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi all, forgive the awkward title. I wasn't entirely sure what to call this one. This isn't really intended to be any sort of proposal, but it's intended to act as sort of a question and sort of a launching point for a discussion.
One of the oft quoted issues I hear with
IFormattableis that it's too widely scoped.formatcan be anything representable by UTF-8 text. "g", "2", "こんにちは", "yyyyMMdd", or even up to just short of 2gb of Shakespeare's writings could be passed to this method. This presents two problems:With .NET 11 just around the corner (RC 1 just released a couple days before the date of writing), this made me think that unions may be able to help solve this issue.
This gets us a step closer - now both the caller and implementing method have an exhaustive list of types. However, I feel there are some issues still:
DefaultFormatproperty inIFormattable<TFormatOptions>.IFormatUniontype that implementsIUnionand has the previously mentionedDefaultFormatproperty.ToString()fromobjectis still available, which could forward usingToString(General, null)or similar.formatis not permitted to be null, which is by design. If this were allowed to be null, it would reintroduce the same issue as before.I'm curious to see if anyone has thoughts on restricting this to just
IFormatUnions, or if that might be too restricting to developers. Also welcoming general thoughts! If this is something that makes sense to do, I may reformat this into a suggestion, but that's not necessarily a goal of this discussion.How I imagine this could work.
All reactions