4 ms·
There are a few different ways of implementing interfaces, each with different tradeoffs, so it's not boilerplate :^)
by kristoff_it 2y ago
There are a few different ways of implementing interfaces, each with different tradeoffs, so it's not boilerplate :^)
- 0x696C6961 2y agoI'm only familiar with the vtable approach, what other ways are there?
- throwawaymaths 2y agofieldparentptr. The zig allocators used to use this because it enabled allocator interfaces without type erasure, but it was found to have a minor but real performance penalty as it is impossible for any compiler to optimize for this in scenarios that are useful for allocators. Other interfaces might actually have the opposite performance preference
- cgh 2y agoIf you control every implementation (ie you aren’t writing a library where others will implement your interfaces), then tagged unions are a simple way to accomplish this. See the bottom of this page: https://www.openmymind.net/Zig-Interfaces/ https://www.openmymind.net/Zig-Interfaces/