4 ms·
Could you expand on the generics point, please? That sounds interesting but I can't quite get my head around it.
by mst 3y ago
Could you expand on the generics point, please? That sounds interesting but I can't quite get my head around it.
- cztomsik 3y agoFunctions in Zig can be called both in runtime and in compile-time. You can force some expression to be called during comptime using a keyword, and sometimes the comptime is implied (like when you define top-level const) If a function is called in comptime, it can also return types. So for example: // this is a function which accepts a type // if you accept type, you also have to restrict the arg to be comptime // if the arg is comptime it still does not mean that the function cannot be called in runtime, // but in this case, it returns type, so it is comptime-only function // there are also cases where this is not true, like std.mem.eql(u8, a, b) which accepts type, // but can be called in runtime because it does not return type fn Wrapper(comptime T: type) type { return struct { value: T }; } const F32Wrapper = Wrapper(f32); // @TypeOf(x.value) == f32 var x: F32Wrapper = .{ value: 1.0 }
- lr1970 3y agoZig has `comptime` where you effectively generate Zig code at compile time by writing Zig code, no special generics syntax/semantics needed. It is very nice and powerful concept that covers lots of ground that in Rust would belong to the land of procedural macros.