Comment by mort96

15 days ago

And you're saying that a modules are instances of structs? Meaning functions in modules are non-static methods?

modules are struct namespaces, so they create a compile time type.

modules (might not mean the same as module in any other given lang) ⊂ imports ⊂ structs ⊂ namespaces ⊂ types

  • Right, that's what I thought. So object member lookup and module/namespace lookup are not the same thing

    • Depends. Your module can have top level member fields or declarations. (By convention these modules would be capitalized) and in that case it would be instantiated as a struct (no objects in zig)

      MyModule.zig

         somefield: u32,
      
         fn get(self: @This()) u32 {
            return this.somefield;
         }
      

      main.zig

         const Module = @import("MyModule"); // note there are build parameters necessary to make this a module
      
         fn main() void {
           var x: Module = .{.somefield = 42 };
      
           _ = x.get() // => 42
      
           ...
      
      

      A module is an import external to your project. Whether or not the import has top level fields or declarations, the mechanism is the same for structs, imports, and modules. For all imports (including modules) you should imagine an implicit `struct{...}` expression surrounding the code in your file.

      3 replies →