5 ms·
> And its caller would need to be generic, and so on. My point though, was that it's extremely similar to code you already write. Zig encourages you to write `
by TakeBlaster16 4y ago
> And its caller would need to be generic, and so on.
My point though, was that it's extremely similar to code you already write. Zig encourages you to write `fn foo(allocator: Allocator, ...)`. That would just become something like `fn foo(allocator_type: anytype, ...)`. Sure one is generic and one isn't, but since types are just normal parameters, it works out to the same amount of typing.
> std::io::Read::read_to_end takes a Vec<u8>
In Rust, read_to_end is part of a trait and appears in a vtable, so it can't be generic. The Zig equivalent `Reader.readAllArrayList` however, is a free function that is statically resolved. Object safety isn't a concern. In fact, Reader already has generic functions, such as `readInt`. `readAllArrayList` could be generic for the same reason that `readInt` can be.
- Arnavion 4y ago>My point though, was that it's extremely similar to code you already write. Zig encourages you to write `fn foo(allocator: Allocator, ...)`. No function that takes an ArrayList(u8) needs to have an Allocator parameter. >In Rust, read_to_end is part of a trait and appears in a vtable, so it can't be generic. That's what my comment already said, yes. >Object safety isn't a concern. ??? It was an example of the downside of Rust's choice. Object safety is a concern for std::io::Read::read_to_end. Rust's choice of having the Allocator be part of the type has forever doomed std::io::Read::read_to_end users from being unable to use any allocator for the Vec other than GlobalAlloc.
- TakeBlaster16 4y agoI'm not sure why you keep steering the conversation to Rust. It doesn't matter how doomed Rust's Read trait is. Zig is a totally different language. Zig is not bound by the same design tradeoffs. That is why it's possible for Zig to have generic functions in its Reader interface, such as today's `readInt`, or tomorrow's `readAllArrayList`.
- Arnavion 4y agoMate, I'm not "steering" the conversation to Rust. The paragraph you responded to was about Rust in the first place. I clearly said there are two downsides of having the Allocator be part of the type. One, the busywork from having to propagate the type parameter, applies to Zig and Rust. The other, the fact that generic methods can't go in vtables, only applies to Rust.