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

Proposal: ability to set type name directly in containers. #2515

Open
tgschultz opened this issue May 17, 2019 · 4 comments
Open

Proposal: ability to set type name directly in containers. #2515

tgschultz opened this issue May 17, 2019 · 4 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@tgschultz
Copy link
Contributor

tgschultz commented May 17, 2019

Zig's ability to generate new types at comptime is an incredibly useful tool, however, in all but the simplest of cases it quickly leads to unreadable type names:

error: no member named 'readFn' in struct 'std.io.OutStream(*const std.os.file.Streams,fn(*const std.os.file.Streams, []const u8) @typeOf(std.os.file.Streams.writeFn).ReturnType.ErrorSet!void)'
                .readFn = writeFn,
                ^

I've personally seen recursively generated types with names several lines long. As one of zig's fundamental tenants is readability and clarity, it would be useful to clean these up. It is unlikely that the compiler can make decisions in this regard that will be adequate for all use cases.

I propose allowing the programmer to set the type name directly from within the container's scope. Let us imagine a new builtin: @setTypeName(comptime name: []const u8).

pub fn OutStream(comptime S: type, comptime WriteFn: type) type {
    return struct {
        @setTypeName("OutStream(" ++ @typeName(S.Child) ++ ")");
        const Self = @This();
        
        impl: S,
        writeFn: WriteFn,

        pub fn write(self: Self, bytes: []const u8) !void {
            return self.writeFn(self, bytes);
        }

        //......

    };
}

Would then display as:

error: no member named 'readFn' in struct 'std.io.OutStream(File.Streams)'
                .readFn = writeFn,
                ^

Drawbacks that spring immediately to mind are that this increases the risk of naming conflicts, and could obscure the source of the type.

@daurnimator
Copy link
Collaborator

Would this get prefixed with the module name? E.g. io.

@tgschultz
Copy link
Contributor Author

That sounds like a good idea, yes. I've updated the example.

@daurnimator
Copy link
Collaborator

The OutStream is implicit too: maybe you should only get to modify the bit in parenthesis?

@tgschultz
Copy link
Contributor Author

I don't think that would cover all use cases. Consider the comptime Pseudo-Struct, where base names would end up being FieldFn or impl.

@tiehuis tiehuis added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label May 21, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone May 28, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants