Skip to content
0:00 / 0:00

#zig#polymorphism#vtables

Runtime polymorphism: why, how? - Alex Naskos

Zig SHOWTIME

17K subscribers • 45 videos on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • Presented by Alex Naskos

Intermediate. Assumes working knowledge.

10K views5 years ago1 recommendation

Programming languagesSystems programmingZigPerformance and optimizationDesign patterns

Alex Naskos explains different ways to implement runtime polymorphism and interfaces in Zig, including vtables.

From Zig SHOWTIME #17 Subscribe to the Zig SHOWTIME Newsletter! zig.show Title Talk Interview

Why it's here

1 person recommends this

What people say. Recommended for understanding how interfaces and runtime polymorphism work behind the scenes in Zig.

Moments people linked to

u/kristoff-itin r/ProgrammingLanguages • Recommends

On “Easy Interfaces with Zig 0.10.0”

In that context "how the system works behind the scenes" is with regards to how interfaces themselves are implemented. There are multiple ways you can implement runtime polymorphism and in Zig it can matter which you pick in some contexts. In the article I linked to this talk that shows many of the different ways interfaces can work: link to this video

u/kristoff-itin r/ProgrammingLanguages • Recommends

On “Easy Interfaces with Zig 0.10.0”

Literally linked at the top of the article: link to this video If you want vtables in Zig, you can have vtables.

u/g41797in r/Zig • Mentions

On “"Zig programmers can implement their own allocators by fulfilling the Allocator interface"”

Zig interface is something misterious see below my *black list*: * https://zig.news/kristoff/easy-interfaces-with-zig-0100-2hc5 * https://zig.news/akhildevelops/learning-interfaces-by-implementing-iterator-in-zig-3do1 * https://zig.news/yglcode/code-study-interface-idiomspatterns-in-zig-standard-libraries-4lkj * https://zig.news/kilianvounckx/zig-interfaces-for-the-uninitiated-an-update-4gf1 * https://github.com/alexnask/interface.zig * link to this video * https://revivalizer.xyz/post/the-missing-zig-polymorphism-reference/ Long story short - DIY

u/kristoff-itVP of Communityin r/Zig • Mentions

On “Allocgate is coming in Zig 0.9, and you will have to change your code”

Sorry my point about inheritance was as an example of something that at one point in time is considered universally good... until it's not anymore. Adding a builtin interface type in Zig binds it to a specific implementation, while there are many ways of achieving runtime polyorphism, tagged unions being one example of that. There's a talk by Alex Naskos that covers a few more ways of doing so: link to this video

u/p_j_zin r/Zig • Mentions

On “Just trying out Zig, want to know how to do COM-style objects”

This video shows a few different ways to get that effect: link to this video

u/CircArgsin r/Zig • Mentions

On “Zig comptime and Rust lazystatic like behavior”

Testing some further comptime related things namely dynamic dispatch inspired by this great video from Zig Showtime I tried to make a minor generalization of one of the examples to have a `call` function that takes a string. I started with this ``` const Dyn = struct { const VTable = struct { call: fn ([]const u8, *usize) void }; vtable: *const VTable, // object: usize, const This = @This(); fn call(self: This, func: []const u8, ctx: *usize) void { self.vtable.call(func, ctx); } fn make(obj: anytype) This { const PtrType = @TypeOf(obj); return .{ .vtable = &comptime VTable{ .call = struct {…
1View on Reddit

u/ChrisTrenkampin r/Zig • Mentions

On “What “sucks” about Zig?”

When I played around with Zig, I investigated a way to implement Go/Java's dynamic dispatch interfaces, and seamlessly use them in a way where you can write functions that accept generic polymorphic interfaces at runtime, or accept a known implementation using the comptime keyword. I used the implementation demonstrated here: link to this video I had a working POC, but what really annoyed me was you still had to manually write your vtable(s) in your structs, and, as far as I could tell, there was no way to automatically generate it with comptime. I'd like a way to generate, or modify, the source code before it gets sent to the compiler, similar to how Java's annotation processor works (except Java o…
1View on Reddit