Remix.run Logo
continuational 9 hours ago

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
rene_d 9 hours ago | parent | next [-]

Optionals handle this in zig:

  var value: ?T = null;
Write:

  value = 10;
Read:

  if (value) |x| x+=1
continuational 9 hours ago | parent | next [-]

Sure, but this is an example from the article, and pertains to sum types in general, not just Maybe.

dnautics 8 hours ago | parent | next [-]

i dont think its generally a good idea to be making complex type generators like this in zig. just write the type out.

the annoyingness of the thing you tried to do in zig is a feature. its a "don't do this, you will confuse the reader" signal. as for optional, its a pattern that is so common that it's worth having builtin optimizations, for example @sizeOf(*T) == @sizeOf(usize) but @sizeOf(?*T) != @sizeOf(?usize). if optional were a general sum type you wouldn't be able to make these optimizations easily without extra information

dirkt 8 hours ago | parent | next [-]

The point is that algebraic data types are common in functional languages. "Maybe" is just an example of an algebraic data type, there's tons more.

If the article says "functional programmers should take a look at Zig", and Zig makes algebraic data types hard, then maybe they shouldn't use it.

If you even say "the annoyingness is a feature, use zig the way it is intended to be used" then that's another signal for functional programmers that they won't be able to use zig the same way they use functional languages.

NobodyNada 7 hours ago | parent | prev [-]

> if optional were a general sum type you wouldn't be able to make these optimizations easily without extra information

Rust has these optimizations (called "niche optimizations") for all sum types. If a type has any unused or invalid bit patterns, then those can be used for enum discriminants, e.g.:

- References cannot be null, so the zero value is a niche

- References must be aligned properly for the target type, so a reference to a type with alignment 4 has a niche in the bottom 2 bits

- bool only uses two values of the 256 in a byte, so the other 254 form a niche

There's limitations though, in that you still must be able to create and pass around pointers to values contained within enum, and so the representation of a type cannot change just because it's placed within an enum. So, for example, the following enum is one byte in size:

    enum Foo {
        A(bool),
        B
    }
Variant A uses the valid bool values 0 and 1, whereas variant B uses some other bit pattern (maybe 2).

But this enum must be two bytes in size:

    enum Foo {
        A(bool),
        B(bool)
    }
 
...because bool always has bit patterns 0 and 1, so it's not possible for an invalid value for A's fields to hold a valid value for B's fields.

You also can't stuff niches in padding bytes between struct fields, because code that operates on the struct is allowed to clobber the padding.

kibwen 2 hours ago | parent [-]

Yes, the care that Rust goes through to ensure that niches work properly, especially when composing arbitrary types from arbitrary sources, shows why you absolutely don't want to be implementing these optimizations by hand.

8 hours ago | parent | prev [-]
[deleted]
8 hours ago | parent | prev | next [-]
[deleted]
nesarkvechnep 9 hours ago | parent | prev [-]

Came to say this. Early in my career I really thought implementing Maybe in any language is necessary but not I know better. Use the idioms and don’t try to make every language something it’s not.

eikenberry 9 hours ago | parent | prev | next [-]

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?

continuational 9 hours ago | parent [-]

I don't think so, where's the extra information in the Zig example?

In Rust, which is arguably also a low level language, it looks like this:

    enum Option<T> {
        None,
        Some(T),
    }
foltik 8 hours ago | parent [-]

Low-level doesn’t mean more information, it means more explicit.

In Zig, that means being able to use the language itself to express type level computations. Instead of Rust’s an angle brackets and trait constraints and derive syntax. Or C++ templates.

Sure, it won’t beat a language with sugar for the exact thing you’re doing, but the whole point is that you’re a layer below the sugar and can do more.

Option<T> is trivial. But Tuple<N>? Parameterizing a struct by layout, AoS vs SoA? Compile time state machines? Parser generators? Serialization? These are likely where Zig would shine compared to the others.

gf000 an hour ago | parent | next [-]

I don't think there is a standardized meaning of 'low-level'. I think a useful definition is that a low-level language controls more/is explicit about more properties of execution.

So zig/c/c++/rust all have ways to specify when and where should allocations happen, as well as memory layout of objects.

Expressivity is a completely different axis on which these low-level languages separate. C has ultra-low expressivity, you can barely create any meaningful abstraction there. Zig is much better at the price of remarkably small amount of extra language complexity. And c++ and rust have a huge amount of extra language complexity for the high expressivity they provide (given that they have to be expressive even on the low-level details makes e.g. rust more complex as a language than a similar, GC-d language would be, but this is a necessity).

As for this particular case, I don't really see a level difference here, both languages can express the same memory layout here.

lmm 4 hours ago | parent | prev [-]

> Option<T> is trivial. But Tuple<N>? Parameterizing a struct by layout, AoS vs SoA? Compile time state machines? Parser generators? Serialization? These are likely where Zig would shine compared to the others.

I don't see how any of that becomes easier in the Zig case. It's just extra syntactic ceremony. The Rust version conveys the exact same information.

foltik 12 minutes ago | parent [-]

It’s precisely not syntactic ceremony. It’s normal Zig running at compile time in which you can program types as values. In Rust (and most other languages) all you get is a highly abstract DSL:

Foo<T> where for<‘a> T: Bar<‘a, baz(): Send>

Information dense, but every new feature needs language design work. Zig lets you express arbitrary logic, loops, conditionals, etc. It’s lower level of abstraction than a type constraints DSL.

For example, adding “the method in this trait is Send” to Rust’s DSL took a whole RFC and new syntax for Rust. The Zig equivalent could be implemented with an if statement on a type at comptime.

Or how about the transformation of an async function into a state machine. Years of work, deep compiler integration, no way to write such transforms yourself. Same with generators, which still aren’t stable. I’d really like to be able to write these things like any other program.

If you don’t want or need to express things at this lower level of abstraction, fair, same reason most people stick to scripting languages and don’t think about memory layout. But “extra ceremony” is really underselling it.

rdevilla 9 hours ago | parent | prev [-]

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