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

Add custom format support for struct/union/enum types #1029

Merged
merged 7 commits into from Jun 4, 2018
Merged

Add custom format support for struct/union/enum types #1029

merged 7 commits into from Jun 4, 2018

Conversation

tgschultz
Copy link
Contributor

fmt.format will now call a custom format fn for struct/union/enum types if one is defined publicly in their namespace and roughly matches the signature:

pub fn format(self: &SelfType, comptime fmt: []const u8, context: var, 
                comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) 
                Errors!void 

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.

@@ -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;
Copy link
Contributor Author

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.

@andrewrk
Copy link
Member

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

@thejoshwolfe
Copy link
Sponsor Contributor

could this be used by a debugger script to pretty print a representation of a runtime value?

@Hejsil
Copy link
Sponsor Contributor

Hejsil commented May 31, 2018

Looks awesome! Can't wait to implement a way to give structs a default formatter >:). Something like this:

const S = struct {
    a: u8,

    const format = defaultFormatter(S);
};

@andrewrk
Copy link
Member

We can also add a default formatter directly to std.fmt. If there's no format function specified, then iterate over the fields, etc

@andrewrk
Copy link
Member

@thejoshwolfe related: #25

@tgschultz
Copy link
Contributor Author

tgschultz commented May 31, 2018

Coincidentally I threw this function together recently. As you can see, the additions of @typeInfo and @field make this sort of thing pretty trivial.

pub fn printStruct(ptr: var) void
{
    const PtrType = @typeOf(ptr);
    const PtrTypeId = @typeId(PtrType);
    if(PtrTypeId != builtin.TypeId.Pointer) @compileError("Arg is not a struct");
    const StructTypeId = @typeId(PtrType.Child);
    if(StructTypeId != builtin.TypeId.Struct) @compileError("Arg is not a struct");
    const struct_name = @typeName(PtrType.Child);
    warn("{} = {{\n", struct_name);
    const info = @typeInfo(PtrType.Child);
    
    inline for(info.Struct.fields) |*field_info|
    {
        const name = field_info.name;
        const FieldType = field_info.field_type;
        warn("  {}: {} = {},\n", name, @typeName(FieldType), @field(ptr, name));
    }
    warn("}};\n");
}

@tiehuis
Copy link
Member

tiehuis commented Jun 1, 2018

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 {}, but providing a debug formatter (e.g. {?}, {#?}) that can be used to inspect types. Reasoning being that having every type be printable would make it trivially easy for unintended internal information of structs to end up in user output. If we use a different format specifier, at least the caller acknowledges this explicitly on their end.

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.

@andrewrk
Copy link
Member

andrewrk commented Jun 4, 2018

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.

@andrewrk andrewrk merged commit 940a854 into ziglang:master Jun 4, 2018
@tgschultz tgschultz deleted the zig-custom-format branch June 4, 2018 15:11
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

5 participants