logoalt Hacker News

continuationaltoday at 4:32 AM3 repliesview on HN

Do you really prefer this:

  fn Maybe(comptime T: type) type {
    return union(enum) {
        value: T,
        nothing,

        const Self = @This();

        pub fn just(the_val: T) Self   { return .{ .value = the_val }; }
        pub fn nothing() Self          { return .nothing; }

      }
    }
Over this?

    data Maybe a = Just a | Nothing

Replies

rene_dtoday at 4:39 AM

Optionals handle this in zig:

  var value: ?T = null;
Write:

  value = 10;
Read:

  if (value) |x| x+=1
show 3 replies
eikenberrytoday at 4:41 AM

This looks like an example of a low level language vs a high level language (relatively speaking). The low level language makes a lot more of what is going on underneath explicit compared to the higher level language which abstracts that away for a common pattern. Presumably that explicitness allows for more control and/or flexibility. So apples to oranges?

show 1 reply
rdevillatoday at 4:36 AM

My old memories of Guava in Java 6 have been triggered.