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: give functions with inferred error sets a more sane type name #21345

Open
NicoElbers opened this issue Sep 8, 2024 · 2 comments
Open
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@NicoElbers
Copy link
Contributor

NicoElbers commented Sep 8, 2024

Zig Version

0.14.0-dev.1509+fb81522e0

Steps to Reproduce and Observed Behavior

foo.zig:

pub fn foo() !void {}

pub fn main() !void {
    const T = @TypeOf(foo);
    const name = @typeName(T);

    @import("std").debug.print("name: {s}\n", .{name});
}

Observe:

❯ zig run foo.zig
name: fn () @typeInfo(@typeInfo(@TypeOf(foo.foo)).@"fn".return_type.?).error_union.error_set!void

This name, albeit correct, does not give me as a programmer any information about the function. If I want to know the returned errors I would have to do more digging, and if I want to log type names for whatever reason, it causes useless unreadable clutter in the output.

Expected Behavior

I would expect either fn () !void or fn () error{}!void.

Changing foo.zig to the following:

pub fn foo() error{}!void {}

pub fn main() !void {
    const T = @TypeOf(foo);
    const name = @typeName(T);

    @import("std").debug.print("name: {s}\n", .{name});
}

Produces:

❯ zig run foo.zig
name: fn () error{}!void

Which seems much more reasonable to me. Especially since this lines up with the function signature.

@NicoElbers NicoElbers added the bug Observed behavior contradicts documented or intended behavior label Sep 8, 2024
@NicoElbers
Copy link
Contributor Author

It seems the long name is hard coded here, I would personally say this name, albeit correct, is a little useless and causes for clutter. But I guess this changes this issue from a bug to a proposal.

@NicoElbers NicoElbers changed the title Unreasonably long typenames for functions with inferred error sets Proposal: give functions with inferred error sets a more sane type name Sep 8, 2024
@nektro
Copy link
Contributor

nektro commented Sep 8, 2024

#19425 has some previous discussion

@mlugg mlugg added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Sep 8, 2024
@mlugg mlugg added this to the 0.16.0 milestone Sep 8, 2024
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

3 participants