Skip to content

Change ArrayList(non-u8).Writer from compileError to empty struct #18351

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

Conversation

michaelbartnett
Copy link
Contributor

Fixes #18350

When it's not valid for ArrayList or BoundedArray to expose a Writer interface:

  • Keep the Writer declaration as a defined but empty type
  • Move the compileError to the writer() function so people still get useful a error at the call site when trying to call e.g. std.ArrayList(i32).writer()

…o empty struct

When it's not valid for ArrayList or BoundedArray to expose a Writer interface:

- Keep the Writer declaration as a defined but empty type
- Move the compileError to the `writer()` function so people still get useful a error at the call site when trying to call e.g. `std.ArrayList(i32).writer()`
@tauoverpi
Copy link
Contributor

It's possible to not expose it at all:

pub usingnamespace if (T == u8) struct {
  pub const Writer = ...;
  ...
} else struct {};

which may be better along with a comment on why it's not available instead of the error. An empty struct for Writer would be confusing as it pretends to have an API which is unavailable.

@michaelbartnett
Copy link
Contributor Author

I like the type not even existing, but in order to achieve that and also keep the helpful compileError appearing when somebody calls writer() it would need to be something like this:

        pub usingnamespace if (T == u8) struct {
            pub const Writer = std.io.Writer(*Self, error{OutOfMemory}, appendWrite);

            /// Initializes a Writer which will append to the list.
            pub fn writer(self: *Self) Writer {
                return .{ .context = self };
            }
        } else struct {
            pub fn writer(_: *Self) void {
                @compileError("The Writer interface is only defined for ArrayList(u8) " ++
                    "but the given type is ArrayList(" ++ @typeName(T) ++ ")");
            }
        };

This totally achieves all the goals, though I'm not sure if folks care about having the double writer function.

I'll update the PR with this approach regardless, in case folks want to see it in context.

@michaelbartnett
Copy link
Contributor Author

Per @ianprime0509's comment on the bug, proposal #6620 to have @typeInfo and @TypeOf not trigger compiler errors would be better.

Closing this PR and the bug, but if 6620 isn't adopted then I think the idiom for excluding types and interfaces in the stdlib should change in some parts to resemble what is in my changes here.

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 this pull request may close these issues.

std.ArrayList(non-u8).Writer being a compileError hinders introspection
2 participants