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: std.interface - better polymorphism support in standard library #10184

Open
iacore opened this issue Nov 20, 2021 · 7 comments
Open
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@iacore
Copy link
Contributor

iacore commented Nov 20, 2021

For new language users from languages with language-level polymorphism support, it's not clear how to achieve polymorphism in Zig. I think it's important in include an easy way to create new type classes in the standard library for discoverability. The implementation for owned and not-owned type classes already exists as a third-party library. We just need to put it in the standard library and use it within std too (e.g. mem.Allocator).

Related issues: #130, #1268, #4567, #9272

@iacore iacore added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 20, 2021
@iacore iacore changed the title Add std.interface Proposal: better polymorphism support in standard library Nov 20, 2021
@andrewrk andrewrk added this to the 1.0.0 milestone Nov 20, 2021
@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Nov 20, 2021
@iacore
Copy link
Contributor Author

iacore commented Dec 31, 2021

std.io.Writer needs this feature. Currently I have to write writer: anytype, which is not ideal.

@iacore iacore changed the title Proposal: better polymorphism support in standard library Proposal: std.interface - better polymorphism support in standard library Dec 31, 2021
@iacore iacore mentioned this issue Dec 31, 2021
11 tasks
@purrie
Copy link
Contributor

purrie commented Jul 21, 2023

I think I'm leaning more towards taking advantage of compile time duck typing for interfaces. Less complications and side issues. What would be better IMO is addition of "@InterfaceOf(interface: type)" builtin which would work exactly like anytype except it would throw a compile error if you passed in anything that wouldn't match the interface type or if you tried to use within the function that uses the interface what isn't declared on it. I think that would be better in terms of not adding unnecessary complexity to the language, take advantage of what's already there and provide additional type safety for both function/library user and writer. Something like this is already emerging in standard library, for example in HashMap's Context or crypto.tls.Client Stream interfaces.

@VisenDev
Copy link

I believe the std reader and writer have been restructured now to be proper interfaces

@WizardUli
Copy link

I believe the std reader and writer have been restructured now to be proper interfaces

What are the 'proper interfaces' you mention? Zig doesn't have interfaces.

@VisenDev
Copy link

What are the 'proper interfaces' you mention?

Interfaces meaning a struct containing a *anyopaque and a function pointer. This is how std.mem.Allocator is implemented.

Zig now has a proper interface for Reader and Writer methods, rather than using comptime duck typing. The old reader and writers have been deprecated

@dgellow
Copy link

dgellow commented Apr 15, 2024

@VisenDev Your link doesn't return any result, did you mean https://ziglang.org/documentation/master/std/#std.io.AnyReader?

@VisenDev
Copy link

@VisenDev Your link doesn't return any result, did you mean https://ziglang.org/documentation/master/std/#std.io.AnyReader?

Zig recently changed the autodoc code. That must have broken my link

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. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

6 participants