-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Reduce the number of format specifiers #9807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here's a use case to consider: in rust-http, I've implemented Delegated derivation, as has been suggested for a few other things, could be appropriate here for some of these types of use cases. Of course, not all number types will be able to be done with just a single trait. Some may need to be done with multiple traits, if they have a fundamentally different representation of the number from what is supported at present. (Even big numbers probably need manual implementations?) |
I was thinking that something simpler could be used. If This way, if you implement your Basically, we should stop thinking of "type specifiers" and think instead of "formatting flags", BTW, the stream IO in C++ also moved away from format specifiers due to the same reasoning - once you overload |
I like the rudimentary type checking. I like the option of giving a succinct, inline hint as to what kind of data you should expect to see plugged in at that spot (even if that hint is not strictly enforced, due to the design of the (To be honest I probably would have gone a different route with the Back to the subject at hand: People who don't like using the type specifiers are free to just implement default and use |
(I do realize that one can use the named parameters feature of |
Type checking isn't bad as of itself (obviously :-) but the existing type specifiers are doing a double duty, one of the type check and another of controlling the base of printed integers; and they don't do a very good job at either role. Today, for type checking, (1) one must implement both For control over base of integer, one must associate the type with a specific base trait, thereby forcing anything that allows control over the base of integers to be grouped in the same "type" as all the numbers in the system. And one needs to associate the type with each and every base trait, without any compiler support for ensuring one did so, or put the burden over the user to know which base(s) are supported and which aren't. It would be cleaner to not mix up the issue of base and the issue of type checking, and do each of these "properly". So how about this: For type checking, allow For type checks, allow Allow combining both base and type checks, e.g. This would be:
Seems like a win-win... |
This issue has been moved to the RFCs repo: rust-lang/rfcs#297 |
Given the existence of
{}
and theDefault
trait, there's no real value in{:i}
,{:u}
,{:t}
, etc. other than for providing very rudimentary type checking for the parameters passed toformat!
.It would be simpler to just use
{}
everywhere and only use specifiers for things like changing the base of printed integers (that is, only keep specifiers such as{:x}
which actually affect the way the printing is done).If specifiers are only used for changing the base of printed integers, then their implementation can be simplified to just setting a current base member of the format which would be available to the implementation of the formatting in the
Default
trait, instead of using a trait per each specifier.This way we'd end up with having to implement just one trait for printing any type, including number-like types, instead of having to implement multiple traits (one per base).
The text was updated successfully, but these errors were encountered: