Skip to content

proposal: differentiate between "field" and "decl" with @field #3839

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

Open
tgschultz opened this issue Dec 3, 2019 · 3 comments
Open

proposal: differentiate between "field" and "decl" with @field #3839

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

Comments

@tgschultz
Copy link
Contributor

This was discussed at the end of #1439.

In that issue the proposed @hasField builtin was split into @hasField and @hasDecl in order to differentiate between struct instance fields and struct namespaced declarations.

It is further proposed that @field either be altered in the same manner: @field/@decl or be renamed to make it clear that it refers to any "member" of a struct regardless of which it is (@member). My personal preference is the former.

related: #1836

@andrewrk andrewrk added this to the 0.7.0 milestone Dec 3, 2019
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Dec 3, 2019
@frmdstryr
Copy link
Contributor

frmdstryr commented Dec 5, 2019

Agree it should be split. Also would be nice to have all three (@member, @field, @decl) and similar has variants.

Edit: Or perhaps it'd be more extensible to specify the type with the has variant? Eg, @hasMember(T, .Field, "name") then a single @member to retrieve it.

Why are these builtins at all instead of just some fn defined in std.meta / std.meta.traits? Just curious.

@tgschultz
Copy link
Contributor Author

IIRC the reasoning is that the 'has' functions in std.meta are inefficient because they require comptime looping over TypeInfo and this case was common enough to warrant a more efficient builtin.

@Mouvedia
Copy link

Mouvedia commented Jun 14, 2021

I stumbled on this today while reading the documentation:

// Structs can have 0 fields.
const Empty = struct {
    pub const PI = 3.14;
};

but

assert(@field(Empty, "PI") == 3.14);

I agree this is really confusing and unexpected for newcomers.
We should have @member for both decls and fields, and have @field and @decl for fields and declarations respectively.

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