4 ms·
Do you really prefer this: fn Maybe(comptime T: type) type { return union(enum) { value: T, nothing, const Self = @This();
by continuational 5mo 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
- rdevilla 5mo agoMy old memories of Guava in Java 6 have been triggered.
- rene_d 5mo agoOptionals handle this in zig: var value: ?T = null; Write: value = 10; Read: if (value) |x| x+=1
- nesarkvechnep 5mo agoCame 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.
- continuational 5mo agoSure, but this is an example from the article, and pertains to sum types in general, not just Maybe.
- deleted 5mo ago[deleted]
- dnautics 5mo agoi 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 5mo agoThe 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.
- dnautics 5mo agozig doesnt make algebraic types hard. algebraic types are exceptionally easy and also safe. and unlike rust, named in a way thats friendly to c devs. zig makes stupid metaprogramming tricks on algebraic types annoying (not hard). so, being precise: zig is not necessarily annoying for fp programmers (my main tool of trade in Elixir). zig is made to be annoying for architecture astronauts.
- NobodyNada 5mo ago> 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.
- deleted 5mo ago[deleted]
- eikenberry 5mo agoThis 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 5mo agoI 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 5mo agoLow-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.
- lmm 5mo ago> 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 5mo agoIt’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. 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.