-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
Now that the Default
trait has been added for handling {}
format, there are two ways to define how to convert an object to a string: foo.to_str()
and format!("{}", foo)
; that is, the object needs to implement both ToStr
and Default
.
The proposal is to eliminate this duplication by eliminating ToStr
and, instead, implementing to_str()
by internally invoking the formatting function. This of course would be a major breaking change to existing code.
An alternative would be to provide a "default implementation" for the Default
trait for anything implementing ToStr
. This would be a backward-compatible change but would require extending the type system to allow for allowing default implementation of traits (if the type implements some other traits) with the ability to override the implementation for specific types.
Activity
alexcrichton commentedon Oct 11, 2013
I think that this is tricky to do. Right now
Default
andToStr
are different semantically in the sense that I don't think that they can be directly merged. What I would be ok with, however, is to do the following:deriving(ToStr)
, change it toderiving(String)
or maybeFmtString
. This would add a deriving implementation of theString
formatting trait (the:s
qualifierToStr
toFmtString
impl<T: fmt::String> ToStr for T
That way nothing else really needs to implement
ToStr
, theto_str
method is just a convenient way of sayingformat!("{:s}", t)
.One of the main things which I would like to avoid is a deriving-like format of the
Default
trait because I don't think that there's fantastic way to do that. What do you think about these steps?orenbenkiki commentedon Oct 11, 2013
You are right, I was actually mixing issue #9807 into the proposal, and I shouldn't have - they are logically separate. Yes, indeed, if we ignore issue #9807, it makes more sense to merge
String
orFmtString
or whatever it is called withToStr
instead of mergingDefault
, and makingto_str()
invokeformat!
using{:s}
instead of{}
. Thanks for catching this.huonw commentedon Dec 7, 2013
I was talking to @alexcrichton on IRC, and he was ok with:
This would still require removing the
deriving(ToStr)
mode.fmt::Default
tofmt::Format
#11298Rename `fmt::Default` to `fmt::Format`
1 remaining item