Hacker News new | past | comments | ask | show | jobs | submit login

I'm actually more puzzled about the infinite recursion in this type function:

  fn Node(T: type) type {
      return struct {
          value: T,
          next: ?*Node(T) = null,
      };
  }
In other languages, defining types in terms of themselves is unproblematic, because the type identifier is just a symbol and the whole thing amounts to a graph with a backreference.

However, here it's supposed to represent actual executable code, which is run by the compiler and "produces" a type in the end. But how would the compiler execute this function without getting stuck in a loop?




That seems wrong? For exactly the reason you say. The correct code, I would guess, should be `@This()`

However, I also wouldn't be surprised if somehow the memoization in the zig compiler makes this ok


No, it's fine because the 'next' struct member is just a pointer which has a known representation.


Ah, so the * type function can "lazily evaluate" its argument?


It's not actually about `*` -- for instance, declaring `const T = *T;` emits an error. The thing that makes this okay is that field types (for structs and unions) are evaluated in the "lazy" way you describe.


Ah, that makes sense. Thank you!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: