I am a beginner programmer. I like rust. The trait system in rust is very powerful. I it would be great if zig introduce trait.
Is there any change. If not can you explain why?
Hello @Omar-xt
The first search result for “trait” is: Traits for Static Dispatch
I think it’s a good thread that links to some explanations.
Everything isn’t out of question, but Zig already has comptime and anytype which is 80% of Rust’s traits.
- Rust’s traits can be used as static interface, which Zig can do this way:
pub fn Interface(comptime Context: type, comptime function: fn (Context, ...) ...) type {
return struct {
context: Context,
// here you put the functions and declarations you want in your interface
pub fn interfaceFunction(self: @This(), ...) ... {
return function(self.context, ...);
}
};
}
fn functionThatExpectAnInterface(interface: anytype) void {
interface.interfaceFunction(...);
}
The problem here is that the anytype parameter doesn’t tell what it expects in the type system. It’s still type-safe, the compiler will complain if a parameter is passed and its type doesn’t allow some operation,but the intent should be clarified in comments, documentation and such, not ideal. It’s in my opinion, the most likely to get a built-in language feature in the future. But Zig folks are generally more wary the cost of abstraction than Rust folks, and it might not that useful to the lanhuahe in the end.
- Dynamic interfaces are similar but there’s a bit more work to do because the functions will take pointers to opaque and will be implemented using pointer-casting:
pub const Interface = struct {
context: *anyopaque,
vtable: *const struct {
function: fn (*anyopaque, ...) ...,
},
// here you put the functions and declarations you want in your interface
pub fn interfaceFunction(self: Interface, ...) ... {
return self.vtable.function(self.context, ...);
}
};
fn functionThatExpectAnInterface(interface: Interface) void {
interface.interfaceFunction(...);
}
This is less likely even than the static interfaces to be built-in, because using virtual functions can hurt performance. So you should be able to do it, but try something else first.
-
Now, traits can also be conditions that types must satisfy. Thanks to type reflection, this can be done in userland (self-promo, hope you don’t mind).
-
The last thing they’re doing is defining behaviour for operator overloading, or iteration in for loops, etc. And that Zig will probably never do because it goes against its philosophy of “no hidden control flow”.
I suggest reading through the many, many, many discussions about comptime zero-cost interfaces (essentially what traits are). You will get a good idea for how zig allows for them. There is also this post by Andrew in regards to a proposal for comptime interfaces:
I’m not saying there won’t be interfaces of any kind, ever, but there are no current plans for adding them, and this proposal is incomplete. It does not propose anything specific.
I suggest if you want to use zig, you accept the fact that it may never gain an interface-like feature, and also trust the core team to add it in a satisfactory manner if it does get added at all.
Which i found on linked to on this thread
I’m not sure i see a reason to keep this thread open anymore, as it’s usefulness is in compiling the above list of links for others to find and dive into before broaching this subject again.