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

[Question / proposal-maybe] resolving the @typeName of types in struct fields (eg. for ad-hoc/non-pkg-based sub-namespacing) #4330

Closed
metaleap opened this issue Jan 30, 2020 · 5 comments · Fixed by #9363
Milestone

Comments

@metaleap
Copy link
Contributor

metaleap commented Jan 30, 2020

Consider:

const std = @import("std");

const Group = .{
    .Type1 = enum{foo, bar, baz},
    .Type2 = union(enum) { a: u16, b: i16 },
};

pub fn main() void {
    std.debug.warn("{}\n", .{ @typeName(Group.Type1) });
    std.debug.warn("{}\n", .{ @typeName(Group.Type2) });
}

Output:

Group
Group

Not terribly critical in the current scheme of things, but would suggest as an "around-or-post 1.0 contributor-friendly rather-nice-to-have" longer-term to look into. (Or when self-hosting-comp is up and such subtleties / corner-cases become more immediately attackable.)

Or maybe it's just an accepted limitation of typeName, then feel free to close. Referencing in the field value another type name instead of anon/inline type-def "works" after all --- although then maybe typeName should rather act like with any other anon/inline type-def not ever anywhere named!

@JesseRMeyer
Copy link

Shouldn't the output be:

enum
union

(or more specialized variants thereof) ?

@metaleap
Copy link
Contributor Author

Yeah what I would expect by default as of today --- although the other, "future proposal/suggestion" half of this issue would be whether to consider a (sugary, admittedly) Group.Type1 or just Type1 etc. Up for discussion. Given that structs are Zig's namespacing device, and that it is unlikely that any callers of @typeName "parse" its results (in the various forms) for extracting some specific meaning.. could be worthwhile + non-harmful.

@JesseRMeyer
Copy link

Not quite seeing the use-case. If you want the parent type, why are you asking about it from its members?

@metaleap
Copy link
Contributor Author

metaleap commented Jan 30, 2020

A use-case? Here's a funky namespace of co-related struct types, to be sep'd out clearly from various other structs in the same .zig file (also not to be scattered into a different .zig file just for the namespacing purposes)

pub const OurApiSpec = .{
    .IncomingRequest = union(enum) {
        negate: fn (Arg(i64)) Ret(i64),
        hostName: fn (Arg(void)) Ret(String),
        envVarValue: fn (Arg(String)) Ret(String),
    },
    .OutgoingRequest = union(enum) {
        pow2: Req(i64, i64),
        rnd: Req(void, f32),
        add: Req(struct {
            a: i64,
            b: i64,
        }, i64),
    },
    .IncomingNotification = union(enum) {
        timeInfo: fn (Arg(TimeInfo)) void,
        shuttingDown: fn (Arg(void)) void,
    },
    .OutgoingNotification = union(enum) {
        envVarNames: []String,
        shoutOut: bool,
    },
};

(Above things sound silly because it's a demo/dummy/test setup for a protocol-handling lib that one then can use to also model real RPC protocols, not mock ones.)

Then later for logging / prints / tracing / compileError messages etc.pp. you want to see either OutgoingRequest or OurApiSpec.OutgoingRequest, not for all 4 types the same OurApiSpec.

@JesseRMeyer
Copy link

Ah, sorry. I got @typeof and @typename confused. I agree the ability to output the name of the identifier, and further context (like enclosing struct name, etc) associated with a type is important for Zig to provide. I don't know, but I would've thought that would be possible already.

@andrewrk andrewrk added this to the 0.7.0 milestone Feb 10, 2020
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
mikdusan added a commit to mikdusan/zig that referenced this issue Jul 12, 2021
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes ziglang#4330
closes ziglang#9339
mikdusan added a commit to mikdusan/zig that referenced this issue Jul 12, 2021
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes ziglang#4330
closes ziglang#9339
mikdusan added a commit to mikdusan/zig that referenced this issue Jul 19, 2021
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes ziglang#4330
closes ziglang#9339
andrewrk pushed a commit that referenced this issue Jul 28, 2021
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes #4330
closes #9339
@andrewrk andrewrk removed the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 28, 2021
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 a pull request may close this issue.

3 participants