-
-
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
Add custom format support for struct/union/enum types #1029
Conversation
Sync with zig-lang/zig master
sync with ziglang
@@ -266,7 +266,7 @@ test "os.time.timestamp" { | |||
|
|||
test "os.time.Timer" { | |||
const ns_per_ms = (ns_per_s / ms_per_s); | |||
const margin = ns_per_ms * 50; | |||
const margin = ns_per_ms * 150; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea why MacOS build on CI isn't hitting this margin.
Thanks for the PR! I'm into this idea. Give me a couple days to go over the changes and merge. Don't worry about the conflict |
could this be used by a debugger script to pretty print a representation of a runtime value? |
Looks awesome! Can't wait to implement a way to give structs a default formatter >:). Something like this:
|
We can also add a default formatter directly to std.fmt. If there's no format function specified, then iterate over the fields, etc |
@thejoshwolfe related: #25 |
Coincidentally I threw this function together recently. As you can see, the additions of
|
Do we want a default formatter for everything or is it better to be more explicit? For example, I like Rust's approach of not providing a default formatter in Anyway, the general idea of custom formatters is good and I agree. This is more an aside and could be discussed on another PR/issue. |
Apologies for the delay, I wanted to get my Pointer Reform commit done before context switching. I'll have a look at this first thing tomorrow. I agree with you @tiehuis about opting in to struct field introspection. |
fmt.format
will now call a customformat
fn for struct/union/enum types if one is defined publicly in their namespace and roughly matches the signature:I say 'roughly' because types stored by @typeinfo() seem to only be able to be checked for equality against themselves and I had to hack in a check on the typename instead for now.
The format function will be passed the type format specifiers that occur within the matching
{}
in the format string to handle however it wants.To make this easier I ended up refactoring the way fmt.format works, moving all the type format specifier parsing out of
format
and into relevant type-specific functions.