3 ms·
> Rust doesn't have variadic functions By design or because nobody has implemented them, yet?
by oblio 6y ago
> Rust doesn't have variadic functions
By design or because nobody has implemented them, yet?
- unrealhoang 6y agoit's a hard design problem, actually. But I think with const generic, we are getting very close, variadic could be: fn foo<const N: usize, T>(varied: [&T; N]) { ... } // usage foo([&a, &b, &c]) foo([&a, &b, &c, &d])
- iudqnolq 6y agoIf I'm reading your intent right that would monomorphize to a slice of a specific N, meaning you couldn't express format!("{}{}", 1, 1.0)
- gpm 6y agoI think you're reading the comment you replied to right, but I don't (completely) agree with your conclusion. There are two magic things the format macro does apart from varargs, it checks the format string against the length and format types of the argument at compile time, and it automatically borrows the arguments. Implementing format with varargs means you need to lose those. Once you lose those calling it looks like format("{}{}", &1, &1.0) under the above proposal the type signature of format would look like fn format<const N: usize>(fmt_str: &str, args: [&dyn Format; N]) -> Result<String, Error>; Where `Format` replaces the various `Display`, `Debug`, etc traits and looks something like this trait Format { fn display(&self, fmt: &mut Formatter) -> Result<(), Error> { // Default implementation of this method return Err(Error::NotImplemented) } fn debug(&self, fmt: &mut Formatter) -> Result<(), Error> { // Default implementation of this method return Err(Error::NotImplemented) } ... } I.e. the trick is that the conversion to a trait object happens (implicitly) on the calling side when you take a reference, so all the arguments are all of the same type.
- iudqnolq 6y agoThat's a much better way of putting it. I was mostly trying to point out the missing dyn, but I never explicitly said that
- gpm 6y agoNobody has implemented them or designed them yet. No obvious reason you couldn't come up with a satisfying design, but there's consensus building work there around "this is a good feature and the right design" as well as implementation work.