Comment

Comment by miki123211

Zig's compile time execution lets you do similar things I believe.

In Zig, structs and modules are equivalent, and type declarations can be manipulated at compile time just like any other value. That, among other things[1], lets you write:

  fn LinkedList(comptime T: type) type {
      return struct {
          pub const Node = struct {
              prev: ?*Node,
              next: ?*Node,
              data: T,
          };
  
          first: ?*Node,
          last:  ?*Node,
          len:   usize,
      };
  }

I wonder if there's anything that OCaml functors can do but this can't.

[1] for example, you can implement a very efficient printf that gives an error at compile time when the format string is invalid. See https://ziglang.org/documentation/master/#comptime for more details.

parent miki123211 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton