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

Expose docComment in @typeInfo data #2573

Open
daurnimator opened this issue May 28, 2019 · 5 comments
Open

Expose docComment in @typeInfo data #2573

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

Comments

@daurnimator
Copy link
Collaborator

daurnimator commented May 28, 2019

This issue did just give me an idea though: what if @typeInfo data included the doc comment? So I could e.g. do:

fn getDef(comptime T: type, field: []const u8) ?builtin.StructField {
    const info = @typeInfo(T);
    const defs = switch (info) {
        builtin.TypeId.Struct => |s| s.defs,
        builtin.TypeId.Union => |u| u.defs,
        builtin.TypeId.Enum => |e| e.defs,
        else => return null,
    };
    inline for (defs) |def| {
        if (mem.eql(u8, def.name, field)) return def;
    }
    return null;
}
fn methodDocs(comptime T: type, field: []const u8) !?[]const u8 {
    const def = getDefInfo(T, field) orelse return error.NoSuchMethod;
    return def.docComment;
}

Originally posted by @daurnimator in #2539 (comment)

@tgschultz
Copy link
Contributor

tgschultz commented May 28, 2019

One thing that was mentioned in IRC is that this would allow storing comptime metadata in comments and changing program behavior based on it. I think we can all agree that comments changing program behavior is exceptionally un-zen.

Something like #1099 would allow a more obvious path for whatever that kind of thing is trying to accomplish, but the possibility would still be there if this feature existed.

@andrewrk andrewrk added this to the 0.5.0 milestone May 28, 2019
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label May 28, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 28, 2019
@vi
Copy link

vi commented Sep 18, 2019

comments changing program behavior is exceptionally un-zen

Doccomments are not normal comments.

Converting text from doccomments to CLI --help message chunks looks like a good idea, and is a program behaviour change.

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@tauoverpi
Copy link
Sponsor Contributor

tauoverpi commented Sep 1, 2020

If @Type(.Struct/.Enum/.Union) ever get decls definition then this is possible through comptime without loss of decls through @src() and @embedFile() abuse by wrapping everything in @Type(.Struct) with a documentation field and later reconstructing the intended type with @Type().

It would be nicer to have it in TypeInfo rather than the hack for generation of things like OpenAPI object specifications if documentation hooks are never implemented.

Edit: added "rather than the hack"

@data-man
Copy link
Contributor

data-man commented Sep 1, 2020

How much memory will the compiler eat with this feature?

@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
@cztomsik
Copy link

cztomsik commented Dec 25, 2023

One interesting use-case for this would be auto-generation of OpenAPI documentation without need for a separate build-step.

We can currently generate schema from type introspection, but we cannot access comments so you can't see what the fields are supposed to mean.

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

7 participants