-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Questions about formatters for std::
entities
#3012
Comments
std::
entities
Right.
I don't think we should need to format anything other than the message by default but we should provide an escaped option similarly to strings.
I would recommend checking what other languages do.
Sure. the
I don't think we should add anything without compelling use cases.
Again, I would recommend checking what other languages do. As usual PRs are welcome =). |
I tried to find similar types in Java, Rust, and C#. Also, we can look at what dynamically typed languages do, Python for example. I can be wrong in some examples here since I'm not a programming languages expert. JavaJava has Optional type, and documentation for
System.out.println(Optional.of("foo").toString()); // outputs Optional[foo]
System.out.println(Optional.ofNullable(null).toString()); // outputs Optional.empty I haven't found any Variant-like type in Java. RustVariants are represented by enum Variant {
Integer(i64),
String(String),
}
fn main() {
println!("{}", Variant::String("foo".to_string()));
println!("{}", Some(42));
} C#C# has Nullable type for Optionals, and it can be formatted this way: Console.WriteLine("{0}", new int?(42)); // outputs 42
Console.WriteLine("{0}", new int?()); // no output, empty line Looks like C# also doesn't have Variant-like types. PythonWith Python everything is simple - every variable is Variant, and also Optional. The idea is to output a value for the variable (using SummaryIt sounds more reasonable for me to apply the same idea as in Python for As for |
Thanks for researching this. Some thoughts:
|
I was thinking about implementing #2977, it seems pretty straightforward, right? Just add a simple
fmt::formatter
withwrite(exception.what())
intofmt/std.h
and call it a day. But there is also the formatter forstd::variant
, it outputs the underlying value asvariant(42)
with escaping string and char values. The other formatter there, forstd::filesystem::path
, outputs"/home"
without anypath
prefix, which just mimicsoperator<<
of course.So, such a long introduction to ask a few questions:
std::exception
have any prefix or escape the what string?std::optional
: should it haveoptional
prefix and also escape the string and char values? Just like the formatter forstd::variant
does.std::source_location
? Since it doesn't haveoperator<<
provided and it consists of 4 components that can be combined in many different ways. For example, it can output using some default format, but what would it be? For me, it'ssource.cpp:line:column
, but who cares what is preferable for me.std::variant
formatter to skipvariant
prefix and an escaping of string and char values? The same decision should also apply to thestd::optional
formatter.std::filesystem
formatter to get a non-escaped path from it?std::stacktrace
(and its entry) if it's currently not testable on CI?And here comes the crazy question:
std::optional
(easy) andstd::variant
(dizzy)? It's like with a range formatter IIRC, treat all specifiers after another:
as underlying specifiers, so the format string{::x}
withstd::optional{42}
would produce2a
(considering that there is nooptional
prefix). This idea works fine withstd::optional
, but forstd::variant
it works only if the format string has underlying specs for the current type of variant.The text was updated successfully, but these errors were encountered: