News for hackers. Stuff that matters.
Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @04:54PM (#34405979)
huh? Every new language has type safe print functions. Having to specify the type with printf formatters is archaic and awful.
Re:Rust vs. C++ Formatting by kccqzy on Monday January 16, 2023 @05:12PM (#34406144)
Most new languages with type-safe print functions are either dynamically typed (which makes this whole discussion moot) or leverage some significant advance in PL theory in order to make the print function type-safe. By advance, I mean not known before the 1990s.
Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @05:46PM (#34406524)
Sorry, I explicitly meant statically typed languages. Rust, Nim, Zig, Odin, Jai, etc.

> For many languages, print is special and the implementation is provided as part of the language implementation.

Sure. Printing variables to strings is definitely worth language-level features imho. It's a bad thing if a language (C++) requires users to come up with extremely complex libraries (fmt/std::format) because the language lacks the features to make such a common operation simple and reliable.

C style printf is a dumpster fire. C++ iostreams are unuseably slow. Modern languages definitely solve this particular problem much better!

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @06:30PM (#34407014)
> Sorry, I explicitly meant statically typed languages. Rust, Nim, Zig, Odin, Jai, etc.

Requiring a macro is no better than what C does. Most of those languages have a less awful macro system than C's, but that's not an actual language advancement. The state of printing is still awful in the languages you mention.

Re:Rust vs. C++ Formatting by dilap on Monday January 16, 2023 @06:57PM (#34407250)
You don't like Zig's solution? I think it's excellent. Normal zig code, executed at compile-time. No magic, no macros, fully compile-time error-checked formatting.

https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L...

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @08:22PM (#34407996)
I don't, no. Proper stage polymorphism is great, but running arbitrary code at compile time without a coherent model of how you're doing that (which ad-hoc const/constexpr/etc. isn't) is like substituting for generics by having dynamic types. If I'm just going to run arbitrary code at compile time I might as well do that with a make rule.
Re:Rust vs. C++ Formatting by dilap on Tuesday January 17, 2023 @08:33AM (#34412160)
Sure, it is basically a dynamic language at compile time, but I feel like the downsides of that are significantly mitigated by the fact that it's running at, you know, compile time -- you'll still get compile-time errors for any mistakes!

(Some people even try to write entire programs in dynamic languages, as crazy as that is.)

Regarding "might as well have a makefile rule," it's not really fair -- in practical terms having a seemless way to evaluate functions in your code at compile time is way different than doing codegen. Like, in practice, how would you write a type-safe format function using codegen? You could do it, but it would be very, very gnarly. Very different.

Even Go -- which famously for a while tried to advocate codegen as an alternative to generics (w/ stuff like stringer to codegen print functions for human names for constants) -- resorts to dynamic types & runtime errors for formatting.

So let's see:

formatting approaches we've seen so far:

– dynamic w/ runtime errors (e.g. Go, C#, C, ...)

– static via baked into compiler support

– static via special-case restricted compile-time evaluated language (e.g. rust macros)

- static via full-lang compile-time eval (e.g. zig)

I think you said all those are awful, so, I'm curious -- what's the better approach you have in mind?

Re:Rust vs. C++ Formatting by lmm on Tuesday January 17, 2023 @05:07PM (#34420094)
IMO the least-bad vaguely mainstream approach is what people do in Haskell with e.g. formatting or fmt. Your formatter should be a first-class strongly typed value, and the way you form/construct that value should be the normal way you form values in your language.

I do think there's space for a "write a complex value literal by writing a string in a DSL and embedding that into the source" feature. But that shouldn't be specific to format strings, and it shouldn't be by running arbitrary code at compile time; rather it should be a language feature. I haven't seen a version of that that I'm really happy with yet, but Haskell's OverloadedStrings or Scala's StringContexts are some baby steps in the right direction.

Re:Rust vs. C++ Formatting by dilap on Wednesday January 18, 2023 @08:19AM (#34426286)
Thanks.

My haskell knowledge is minimal, but looking at formatting and fmt library examples, what stands out is you're operating in the syntax of the language, so it's clumsier to use (imo) than a string with interpolations. (It seems not-dissimilar to C++ streams, to me.)

I guess your second paragraph gets at that. I think you're arguing for, "build it into the language, but flexibly." Fair enough!

Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @07:09PM (#34407350)
> Requiring a macro is no better than what C does.

I disagree in the strongest, most emphatic way possible.

As a user of programming languages the difference between printing in C and printing in Rust is night and day. Whether that is achieved with a powerful macro system or with the core language does not matter to me in the slightest. It carries zero weight.

Writing code that prints variables in C is painful and bad. Writing code that prints variables in Rust is easy and good. If you'd like to say that "easy and good" is the same as "painful and bad" then I disagree.

When std::format ships the gap between C++ and Rust will shrink dramatically. However Rust will still have an advantage with derive macros. C++'s lack of reflection continues to be a major pain point. Maybe in C++29.

I don't think you can fully separate "language" from "macros". If something can be implemented easily in a macro then there is less reason to bake it into a new feature in the language. I don't think it's a good idea to add language features solely so you can say it's a language feature and not a macro feature. YMMV.

Re:Rust vs. C++ Formatting by lmm on Monday January 16, 2023 @11:53PM (#34409289)
A language that makes it easy to represent how to process a potentially complex but semi ad-hoc structure in a coherent way is interesting. A language that's added a special case for when the processing you want to do happens to be printing is much less so. Derive is a poor substitute for a proper record system.
Re:Rust vs. C++ Formatting by forrestthewoods on Tuesday January 17, 2023 @01:33AM (#34409715)
Maybe. But a poor substitute is radically superior alternative to literally nothing.
Re:Rust vs. C++ Formatting by int_19h on Monday January 16, 2023 @07:37PM (#34407614)
If the result is type safe and the syntax is consistent with the rest of the language, the users won't and shouldn't care whether it's implemented as a builtin, as a macro, or as some other kind of magic.
Re:Rust vs. C++ Formatting by xigoi on Tuesday January 17, 2023 @01:36AM (#34409724)
Nim's implementation doesn't require a macro.
Re:Rust vs. C++ Formatting by atq2119 on Monday January 16, 2023 @06:02PM (#34406700)
Rust's print! implementation is pretty darn complicated, wouldn't you say?
Re:Rust vs. C++ Formatting by xigoi on Monday January 16, 2023 @06:17PM (#34406864)
Re:Rust vs. C++ Formatting by elcritch on Monday January 16, 2023 @06:36PM (#34407063)
The magic that makes Nim’s print / repr functions really neat is the `fieldPairs` iterator that works on any type. It’s a comptime function to iterate fields of a type, very handy and very simple to use. It can work on any type unlike Rust’s Debug macro which only works on types you own and can annotate.

https://nim-lang.org/docs/iterators.html#fieldPairs.i%2CS%2C...

Re:Rust vs. C++ Formatting by forrestthewoods on Monday January 16, 2023 @06:20PM (#34406901)
I don't know what Rust's print! implementation looks like. I also don't care. I am not a Programming Language programmer. I am a user of programming languages.

Using C-style printf sucks balls. It's extremely error prone and it doesn't support complex types. Using Rust's print system is delightful. I can't make a type error and arbitrarily complex types can be printed with roughly zero effort via {:?} and #[derive(Debug)].