Hacker Newsnew | past | comments | ask | show | jobs | submit | more 2c2c2c's commentslogin

problem is that the language started hitting its stride and getting attention while the core team is on a 2 year side quest of rewriting the compiler + incremental compilation


it's a pretty narrow set of people that want or need to be able to memorize and recall a bunch of facts as efficiently as possible. mostly med school students and language learners, where this stuff obviously works.


Working in a fast changing but also wide and deep field like software I think being able to remember quite a few facts can be important.


it's doable I guess. I think it's more productive to learn a concept and be able to derive everything about the concept from first principles instead though


somethign like this I think. i only dabble in zig/systems stuff so there might be better/more idiomatic ways to write parts

  const std = @import("std");
  
  // base struct
  const Animal = struct {
      // points to the derived struct
      ctx: *anyopaque,
      // points to the vtable of the concrete type
      vtable: *const VTable,
  
      // the vtable interface derived struct must implement
      const VTable = struct {
          make_noise: *const fn (ctx: *anyopaque, loudness: u32) anyerror!void,
      };
  
      // call the derived struct's implementation
      pub fn make_noise(animal: Animal, loudness: u32) anyerror!void {
          return animal.vtable.make_noise(animal.ctx, loudness);
      }
  };
  
  const Dog = struct {
      // extra data
      weight: u32,
  
      // implement the interface
      const vtable = Animal.VTable{
          .make_noise = &make_noise,
      };
  
      pub fn make_noise(ctx: *anyopaque, loudness: u32) anyerror!void {
          const dog: *Dog = @alignCast(@ptrCast(ctx));
          std.debug.print("woof {} {}\n", .{ dog.weight, loudness });
      }
  
      // helper to convert to the base struct
      pub fn _animal(self: *Dog) Animal {
          return Animal{
              .ctx = @ptrCast(self),
              .vtable = &vtable,
          };
      }
  };
  
  const Cat = struct {
      weight: u32,
  
      const vtable = Animal.VTable{
          .make_noise = &make_noise,
      };
  
      pub fn _animal(self: *Cat) Animal {
          return Animal{
              .ctx = @ptrCast(self),
              .vtable = &vtable,
          };
      }
  
      pub fn make_noise(ctx: *anyopaque, loudness: u32) anyerror!void {
          const cat: *Cat = @alignCast(@ptrCast(ctx));
          std.debug.print("meow {} {}\n", .{ cat.weight, loudness });
      }
  };
  
  pub fn main() !void {
      var gpa = std.heap.GeneralPurposeAllocator(.{}){};
      const alloc = gpa.allocator();
  
      // list of base structs
      var animal_list = std.ArrayList(Animal).init(alloc);
      defer {
          for (animal_list.items) |animal| {
              if (animal.vtable == &Dog.vtable) {
                  const dog: *Dog = @alignCast(@ptrCast(animal.ctx));
                  alloc.destroy(dog);
              } else if (animal.vtable == &Cat.vtable) {
                  const cat: *Cat = @alignCast(@ptrCast(animal.ctx));
                  alloc.destroy(cat);
              }
          }
          animal_list.deinit();
      }
  
      for (0..20) |i| {
          if (i % 2 == 0) {
              var dog = try alloc.create(Dog);
              dog.* = Dog{ .weight = @intCast(i) };
              try animal_list.append(dog._animal());
          } else {
              var cat = try alloc.create(Cat);
              cat.* = Cat{ .weight = @intCast(i) };
              try animal_list.append(cat._animal());
          }
      }
  
      // meows and woofs here
      for (animal_list.items) |animal| {
          try animal.make_noise(10);
      }
  }
ive written a couple and still find them mindbendy


You can just used tagged enums and the inline else syntax, like this:

  const Animal = union(enum) {
      cat: Cat,
      dog: Dog,

      pub fn make_noise(self: Animal) void {
          switch (self) {
              inline else => |impl| impl.make_noise(),
          }
      }
  };


iirc there's multiple idioms that are used in different cases. i recall a nice github that laid them all out with use cases but I can't find it


humble request to make marks for the number key registers global marks

they're global marks in vim. no one uses them there because those registers are reused for clipboard history

the vscode extension doesn't implement registers this way so they're safe to use. i imagine zed's implementation is similar


no idea why it was done, but iirc people still wrote it until gurus started emphasizing that you shouldn't around c++11. around that time there was a strong push to differentiate code from looking like "C with classes"


would make sense to reuse warden for Activision IP post merge


> But Instagram also actively censors criticism of Israel

I have friends working for nonprofits in this space, and I can assure you the 25 stories a day they blast out are not being censored


there's an obvious national security angle to this


i thought berkeley was using a modified version of the book using python a few years back


80% of republicans believe 2020 was stolen.


don't worry, community notes on Twitter will fix this /s


Reddit's censorship surely will.


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

Search: