5 ms·
Interfaces/traits is syntactic sugar for a function dictionary/vtable. It could probably be done via Zig's compile time subset.
by zozbot234 3y ago
Interfaces/traits is syntactic sugar for a function dictionary/vtable. It could probably be done via Zig's compile time subset.
- lenkite 3y agoStandardized language-defined syntactic sugar is deeply important - esp for something as important as interfaces/traits - which are the foundation blocks for setting component contracts in libraries and modules.
- colonwqbang 3y agoNot if the standardised way is worse than the naive way, like in C++. Then I will rather take manual virtual calls, like in C: https://elixir.bootlin.com/linux/latest/source/include/linux/fs.h#L1916 https://elixir.bootlin.com/linux/latest/source/include/linux... It isn't that much more work and it's not something you end up doing terribly often. I think destructors are a much more important to help day to day development and avoid bugs.
- steveklabnik 3y agoIn Rust, traits can be used via a vtable, but also (and the vast majority of the time) as regular old statically dispatched functions. That doesn't mean Zig couldn't do something similar if they want to, just pointing out that it's not always the case that it means dynamic dispatch.
- zozbot234 3y agoI didn't mention run-time dynamic dispatch, only a function dictionary (which C++ calls a vtable). Using traits for bounded subtype polymorphism is much like having a dictionary of functions that can be resolved completely at compile time, making it possible to use monomorphization and static dispatch.
- steveklabnik 3y agoOkay. It's clear we're using some words in very different ways (this is not subtype polymorphism in Rust either) but I see what you're getting at.