Skip to content
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

Remove hacks from ifmt and introduce fprintf #8637

Closed
wants to merge 4 commits into from

Conversation

alexcrichton
Copy link
Member

Recent improvements to &mut Trait have made this work possible, and it solidifies that ifmt doesn't always have to return a string, but rather it's based around writers.

@huonw
Copy link
Member

huonw commented Aug 20, 2013

Could these be called writef or something like that? (Since they definitely aren't tied to files.)

@alexcrichton
Copy link
Member Author

Yeah I'm not really attached to the name. I figured that we chose fmt instead of sprintf, so I tried to keep along the same lines to avoid fprintf as the macro name. If we do something as long as writef perhaps we should just go with fprintf.

The end result of this would be fmtf, but that doesn't really convey what we want, and ffmt looks kinda silly.

@huonw
Copy link
Member

huonw commented Aug 20, 2013

I was purely "arguing" that the first f in fprintf implies files, while this uses any Writer, so a name that indicates this would be more appropriate.

@alexcrichton
Copy link
Member Author

Oh, nevermind then! I kinda like the C-like names, but thankfully those are a lot easier to change than the macro names. In theory no one's actually using sprintf/fprintf by hand...

@graydon
Copy link
Contributor

graydon commented Aug 21, 2013

Yeah, I think writef or write_fmt is probably best here.

r=me with that change though.

@alexcrichton
Copy link
Member Author

I was thinking about the names of these functions, and I figure that it'd be nice to get them right this time instead of having to revert back later. Here's what I've found:

Today we have fmt, and it returns a ~str instance. We also have printf and printfln which write to stdout (currently by allocating a string, and then writing). What we'd like to make room for is somewthing which also writes to a &mut io::Writer.

There's basically three operations we want to support: formatting into a string, formatting onto stdout, and formatting into a writer. I looked at a few other languages to see what they do

  • go - fmt.Printf, fmt.Fprintf, fmt.Sprintf
  • python - no support from what I could tell, only sprintf support through the format method on strings or the % operator
  • C - printf, fprintf, sprintf
  • java - System.out.printf, maybe a PrintStream.printf, and String.format
  • ruby - printf and sprintf, couldn't find an fprintf equivalent.

There's a pretty clear pattern here, and I'd like to not deviate too far from it. Right now, if we wanted to keep fmt as a base, I'd vote to move towards fmt/ffmt/sfmt to print to stdout/stream/string. Sadly though, ffmt looks a bit silly, and so does sfmt.

With those in mind, what would others think about moving directly to printf/fprintf/sprintf as the macros? The pro is that everyone's already familiar with them and knows exactly what they do. The con is that it's a longer name than fmt. Also brought up is that fprintf doesn't necessarily imply "all writers", but staying in the tradition with other languages I'd still vote to call it fprintf.

Thoughts?

@pcwalton
Copy link
Contributor

+1 on printf/fprintf/sprintf. I've wanted those for a while.

@adrientetar
Copy link
Contributor

I don't see ffmt/sfmt as silly (aesthetics consideration?).

@alexcrichton
Copy link
Member Author

@adridu59, the main reason I don't like fmt/ffmt/sfmt could be because I'm already tainted by what rust does, but fmt just seems to imply that it returns a string more than writing to stdout. With printf it's super clear that it's going to standard out and there's no ambiguity there.

@bluss
Copy link
Member

bluss commented Aug 23, 2013

fprintf is a very confusing name for a newcomer to programming, but I guess nobody will fault Rust for sticking with the same identifiers as other established languages.

@adrientetar
Copy link
Contributor

@blake2-ppc Do you have a better suggestion?
Rust is very much a "move-forward" language where it needs to, if there's a better name than before I think it should be taken.

To add to the references, the D doc is there: http://dlang.org/phobos/std_stdio.html#.File.write

@bluss
Copy link
Member

bluss commented Aug 23, 2013

I have a suggestion — since you asked, I guess I am responsible to have a something to contribute together with my complaint. I will say that acrichto's summary misses python's print, while it doesn't do the interpolation, it is the main output function.

Keep it simple, and be very deliberate with which functions or macros appear in the prelude and the default namespace. Remove std::io::{print, println} from the Prelude.

Use macros print!(), println!() and write!(), and have them in the default namespace.

/* mockups, just so that we can try the interface for real.
 * not the actual implementation, of course. */
macro_rules! print(
    ($($A:expr),*) => ( write!(std::io::stdout(), $($A),*))
)
macro_rules! println(
    ($($A:expr),*) => ( std::io::stdout().write_str(ifmt!($($A),*) + "\n"))
)
macro_rules! write(
    ($B:expr, $($A:expr),*) => ( ($B).write_str(ifmt!($($A),*)))
)

fn main() {
    print!("hi {}, ", "adridu59");
    println!("nice to meet you.");
    write!(std::io::stdout(), "hi times {}\n", 3);
}

For "sprintf" I'm unsure, maybe it can be write_str!() or print_str!(), or even format!()?

@alexcrichton
Copy link
Member Author

After some discussion on IRC, sounds like we're all in agreement with @blake2-ppc. The changes this will make:

  • ifmt => format
  • Alter print and println to use new formatting
  • Introduce write and writeln macros
  • Remove the print and println functions from the prelude.

These new macros are all based on format! instead of fmt! and purely exist for
bootstrapping purposes. After the next snapshot, all uses of logging will be
migrated to these macros, and then after the next snapshot after that we can
drop the `2` suffix on everything
bors added a commit that referenced this pull request Aug 25, 2013
Recent improvements to `&mut Trait` have made this work possible, and it solidifies that `ifmt` doesn't always have to return a string, but rather it's based around writers.
@bors bors closed this Aug 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants