News for hackers. Stuff that matters.
Re:MicroZig: Unified abstraction layer and HAL for Zig on se... by GuestHNUser on Thursday February 29, 2024 @01:36PM (#39553169)
> - The lack of first class interfaces/traits/typeclasses is not my favorite. The currently suggested alternatives are so un-ergonomic I'd almost call them hostile.

Personally, I don't see this as a problem. I like implementing an interface in Zig the same way I implement one in C -- I use a function pointer that takes a packet that defines the operation to execute with that packet data. Zig's exhaustive switch statements make these even better to work with.

Here is some pseudocode to illustrate what I mean:

fn doThing(ThingPacket p) switch(p.Opcode) MyInterfaceFunc1_Opcode => return MyInterfaceFunc1(p.Func1Params); MyInterfaceFunc2_Opcode => return MyInterfaceFunc2(p.Func2Param); ... default => error.InvalidOpcode;

There are no comments on this comment yet.