Change ArrayList(non-u8).Writer from compileError to empty struct
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()
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.
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.
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.