Zig, Skia, Clojure, Geometry and the Japanese TV Show: ICFP Contest 2021
tonsky.me
tonsky.me
I would argue however that Rust counts as a functional programming language, however. Algebraic data types, higher-order programming and immutability are all idiomatic features of the language.
Except the first price column clearly shows that is false.
It is quite obvious it was more a skill of the developer getting the first prize than the language itself.
Only if you assume that "number-crunching and brute-forcing" is the only way to the first prize, surely?
> defer is pure genious
Being able to defer for resource cleanup is nice. I like that Zig makes it a priority to make things adaptable (not sure that's the right word). For example, everything in the std lib that allocates, takes an allocator object, so if you want to implement a custom allocator, it's very simple to use. If you want to use a custom event loop for async, it's easy to substitute. All in all, I think its shaping up to be a nice language, just not for me.
try std.fs.cwd().openFile(path, .{}) |file| : file.close()
{
// ... code ...
// file.close() is called at the end
} {
var file = try std.fs.cwd().openFile(path, .{});
defer file.close();
// ... code ...
// file.close() is executed here
}
would work just fine. try std.fmt.allocPrint(allocator, "problems/{}", .{ id }) |path| : allocator.free(path) {
try std.fs.cwd().openFile(path, .{}) |file| : file.close() {
try file.stat() |stat| {
try allocator.alloc(u8, stat.size) |contents| : allocator.free(contents) {
// do something with 'contents'
}
}
}
}
In Zig you can write this, and mess everything up: const path = try std.fmt.allocPrint(allocator, "problems/{}", .{ id });
var file = try std.fs.cwd().openFile(path, .{});
const stat = try file.stat();
var contents = try allocator.alloc(u8, stat.size);
defer allocator.free(contents);
defer file.close();
defer allocator.free(path); eval("Ax + v", {.A = …, .x = …, v = …})
Wouldn’t that be cool?I'm a bit sad there seems to be no interest in adding interpolation syntax to Zig. While anonymous tuples work well for shorter expressions, they can get confusing for larger textual templates or DSLs, imo.
I've been experimenting with Zig for WASM, for example, and the readability of HTML templates suffers. I'd love to be able to pass arguments inline, and while we can do that with anonymous tuples, the added noise makes it annoying to use in practice. I believe Javascript's template literals, out of all things, would be a fitting inspiration for a little syntactic sugar on top of anonymous tuples:
var tuple_params = html.create("{} ... {} ... {}", .{a, b+c, observable});
var tuple_inline = html.create(.{"", a, " ... ", b+c, " ... ", observable, ""});
// equivalent to tuple_inline
var tuple_sugar = html.create(`{a} ... {b+c} ... {observable}`); var tuple_params = html.create("{[foo]} ... {[bar]} ... {[observable]}", .{
.foo = a,
.bar = b+c,
.observable = observable,
});
It's verbose and explicit to be sure, but quite readable IMO.OK, but how much code is it? I doubt even a game engine has matrix operations in more than 0.1% of its lines of code (obviously, in Matlab/Julia it can by 99%). The beauty of so little code is a worthy sacrifice for the explicitness of a low-level language.
I like Zig overall, but lack of operator-overloading just seems like a pain to me.
Zig in many ways puts faith in the coder to not screw things up, but in this case it diverges from that philosophy. Would it be too much to trust the user not to overload operators in an unacceptable way? Social convention over technical constraint.
That said I'd be more persuaded by the argument that it's not worth the extra code and complexity for a language as concise as Zig.
https://www.godbolt.org/z/zWGfcY5a4
Whether this code translates to SIMD or scalar operations under the hood should depend on the target platform and compiler options.
It is a pain, but all things considered, having it would have been a greater pain. Note that the main issue with operator overloading in Zig isn't so much the operator part but the overloading part, because overloading introduces ambiguity that cannot be resolved at the code-unit level. Zig doesn't allow name overloading even for ordinary identifiers (actually, Zig is more strict in its opposition to overloading than Clojure or Erlang, which do allow it when there can be no ambiguity). I think it's more likely that Zig will allow user-defined infix operators (say +' or something) before it allows overloading of any kind.
> Zig in many ways puts faith in the coder to not screw things up
I understand Zig very differently. Zig puts a lot of effort to make it hard for the coder to screw up. Even where it doesn't enforce correctness at compile time or at runtime (and it does both much more than C++, to the point that the goal is to eliminate all or nearly all undefined behaviour in safe mode), its strong emphasis on correctness, including functional correctness, is expressed precisely through a simple and lean language that's easy to understand, so that it's harder for the programmer to screw up (plus fast compilation and easily isolated code units). So to the extent that Zig puts faith in the coder (less than C++, more than Rust), it can do so because the language is so lean and simple.
Can you explain this a bit? Operator overloading just seems like such a strange bugbear of Zig folks to me. Maybe I've just never been bitten by it, but at least in Elixir, my main language, I've never really understood the danger.
Are operators truly so different from functions? You'd expect that any function you import could do, well, anything. Aren't operators essentially just the same thing, just with a "prelude" in the language that basically imports them by default?
In Elixir, you might have (discouraged):
defmodule MyMod do
import MyOperators
and then you know to check what's defined by MyOperators, for that module. But more common, if you really wanted to overload, would be to be more specific: defmodule MyMod do
import MyOps, only: [+: 2]
and then you know plus (with two arguments) has a different meaning in that module.> The very concept of operator overloading is incompatible with such a guarantee.
Why would operator overloading allocate memory? The memory is already allocated(by the user) when you call with the operands.
What if I wanted to overload the + operator to concatenate strings?
There is definitely some merit in being puritanical about not having any allocations in operators.
Not operator overloading; just overloading. You can't overload ordinary-named subroutines in Zig either.
> and then you know plus (with two arguments) has a different meaning in that module.
That's not overloading, that's shadowing. Overloading is when the same name refers to multiple things depending on the type of its arguments. Erlang allows overloading based on arity, but that's something that can be understood from the call-site code.
This seems clearly false to me. Operator overloading is non-virtual type-based dispatch, just like the dot operator for method calls in C++/Java/etc., and is resolved statically at compile time. You can think of overloadable "+" as just syntactic sugar for an ".add()" method (in fact, this is more or less exactly how it's implemented in Rust). Even if your language doesn't have methods, you can implement overloading with typeclasses, like Haskell does (as does Rust, formally speaking).
We could certainly argue on whether disallowing overloading is helpful, but it's first important to understand what the principle is, and it's not about the operators.
No, it doesn't. Knowing what function "+" resolves to requires only the type of the left-hand side (or, I think, the RHS as well in Haskell). Absent overlapping instances (which Rust doesn't support, and Haskell only supports with an extension), there can only be one such function implementation per type. And, since top-level items are fully type-annotated in Rust (and idiomatically annotated in Haskell), this is a completely local determination. Therefore, for every occurrence of "+" in the source, it's completely unambiguous which implementation "+" calls, and this can be determined solely by looking at the calling function.
The end result: Rust, like Zig, disallows overloading of names, but Rust also effectively supports overloaded operators via traits (typeclasses).
Right, and that's more than one definition per name. In Zig, a name refers to only one definition. (Plus, recall that in Zig the type of a target isn't always known when reading the code, probably more frequently than in C++ or Rust, where this only happens in macros.)
> disallows overloading of names
That the definition referred to by an identifier depends on the type is what overloading means. Overloading is the very essence of traits.
> in C++ or Rust, where this only happens in macros
Sorry, this is wrong. In both of these languages it happens much more frequently. All the time, in fact, whenever you either infer return types or just chain calls on return values, or when the type is a generic/template type parameter. So when you see `foo(e)` or `e.foo()`, where `e` is some expression, you can only tell which definition of `foo` is referenced -- or even whether two instances of `foo` refer to the same definition or not -- by figuring out the type of `e`, which is often implicit in the code.
> Overloading is the very essence of traits.
Which is why in Zig, the mechanisms that perform a similar task to that of traits -- i.e. abstracting over types -- work differently, without overloading.
As a general rule, Zig does not want the semantics of a piece of code to depend on things the compiler might know but the reader of the unit might not. Whether or not such a degree of explicitness is a worthy principle for a low-level language is a matter of taste, I suppose.
I proposed this and it got rejected by the language committee.
Say you have this (pseudocode):
function foo() {
let bar = make_bar();
defer cleanup_bar(bar);
let baz = make_baz();
defer cleanup_baz(baz);
let quux = make_quux();
defer cleanup_quux(quux);
do_something_with(bar, baz, quux);
}
Now imagine you have multiple functions like this that all create bar, baz and quux's. Say these are unit tests and the bar-baz-quux are mocks or whatever.So you decide to DRY by moving the creation to a common function.
function create_mocks() -> (Bar, Baz, Quux) {
let bar = make_bar();
defer cleanup_bar(bar);
let baz = make_baz();
defer cleanup_baz(baz);
let quux = make_quux();
defer cleanup_quux(quux);
(bar, baz, quux)
}
... except this doesn't work any more, because the cleanup happens within create_mocks and create_mocks ends up returning cleaned-up values.You could fix this by switching to a callback approach:
function run_with_mocks(f: function(Bar, Baz, Quux)) {
let bar = make_bar();
defer cleanup_bar(bar);
let baz = make_baz();
defer cleanup_baz(baz);
let quux = make_quux();
defer cleanup_quux(quux);
f(bar, baz, quux)
}
... but now you have to make the caller use a callback that necessarily returns void and not any other type. (This is mostly a golang problem, since languages with generics can be generic on the return type of the callback.) It also means you can't easily do things like set variables in an outer scope easily, depending on how the language deals with closure captures.Worse, if not all tests use the same mocks, you actually end up with multiple of these functions:
function with_bar(f: function(Bar)) {
let bar = make_bar();
defer cleanup_bar(bar);
f(bar)
}
...
function foo() {
with_bar(bar => {
with_baz(baz => {
with_quux(quux => {
do_something_with(bar, baz, quux);
});
});
});
}
All these problems are avoided by having destructors.Go doesn't have generics so you can't really do this, but in the case of zig, your function would be parametrized on the type, so you would issue it a struct to be shared by the tests that has the common initialization upfront, make_ functions that return the pre-initialized stuff, followed by running the function, followed by no-op deinits, followed by whatever forensics you'd like to run later.
This is missing the point. The point of a destructor is that it runs automatically. .deinit() is the same as the three cleanup_*()s in my example and thus has the same problems.
>but in the case of zig, your function would be parametrized on the type, so you would issue it a struct to be shared by the tests that has the common initialization upfront
The third example in my post shows why this is inadequate.
Have you tried doing what you propose in zig? Ultimately, the only difference between a .deinit() and a destructor is that one is explicit and the other is not. There are cases where you can easily make a mistake for both an explicit destructor and an implicit destructor.
func make_foo() *Foo {
return &Foo {
bar: make_bar(),
baz: make_baz(),
quux: make_quux(),
}
}
func cleanup_foo(foo *Foo) {
cleanup_bar(foo.bar)
cleanup_baz(foo.baz)
cleanup_quux(foo.quux)
}
// elsewhere
foo := make_foo()
defer cleanup_foo(foo)
// [rest of logic goes here]Poor man's:
func make_foo_with_mock_bar() *Foo {
return &Foo {
bar: make_mock_bar(),
baz: make_baz(),
quux: make_quux(),
}
}
Explicit injection: func make_foo(bar: Bar, baz: Baz, quux: Quux) *Foo {
return &Foo {
bar: bar,
baz: baz,
quux: quux,
}
}
Or use some DI frameworkEven if you have reservations about where verbosity lies, I don't see how this affects the cleanup side.
So if bar, baz, quux aren't intrinsically related as a unit and might be mixed and matched arbitrarily, you just forego the whole ceremony and call each thing or the respective mock individually (the argument being that if you're making multiple different permutations, you're not repeating yourself per se; e.g. mocks don't need to be cleaned up in the first place)
There's also something to be said about tests needing several arbitrary mocks. Normally, I'd consider that a smell. I've actually seen the pattern of making a kitchen sink foo for scenarios like that, but with nils/noops for unused things (can't say I like that approach though).
(https://smartlogic.io/podcast/elixir-wizards/s6e6-matthias-l...) if you're curious
With the three mocks I gave, there are 2^3 - 1 = 7 possibilities of which mocks a particular test needs. Of these, three are degenerate cases of a single mock each which would be served by the individual make_* already, so that leaves four cases of make_foo* that you'd need to implement.
With more than three mocks, this number grows rapidly.
it does in zig:
fn create_mocks() !struct {bar: Bar, baz: Baz, quux: Quux} {
var bar = try make_bar();
errdefer cleanup_bar(bar);
var baz = try make_baz();
errdefer cleanup_baz(baz);
var quux = try make_quux();
errdefer cleanup_quux(quux);
return .{
.bar = bar,
.baz = quux,
.quux = quux,
};
}Your initial pretense was that you always make 3 objects, so move it into a function.
When people show how to accomplish that, you state 'well what about my test that doesn't need all three?'
Why are you testing things your code doesn't do?
You've phrased this as if I said this after I'd been given a solution for the initial problem. In reality, I mentioned this in the original post itself, but it seems many people didn't read it.
It's almost like I have prior real-world experience of this problem, where I already went down the path of making common functions for all the creation and cleanup respectively, only to find that it doesn't help when all the functions don't create the same subset of things.
>Why are you testing things your code doesn't do?
I have no idea how you came to this conclusion.
I've been a Go mentor for years, and see how people take concepts they are used to and try to shoehorn them in, and complain they are ugly.
In this case, when I first saw your examples, my thought was 'youre doing it wrong'. But not wrong as in incorrect, but now how the language was designed.
If you truly care we can go through scenarios and I can tell you how I'd approach it. But in my past experience, when people find a paradigm they like, they tend to not listen.
I think people have a bias against destructors because of C++, but they really are pretty nice. Speaking as a guy that uses C instead of C++, destructors are the one thing I would add to C.
It's understandable why a language trying to be close to C in spirit does not have them.
And you can call close like: file.close().await.unwrap(); https://github.com/tokio-rs/tokio-uring/pull/17/files#diff-b...
It does make things much easier.
func newMocks(
) (
foo Foo, bar Bar, baz Baz,
cleanup func(),
)
and making the caller responsible for calling the cleanup function, possibly in a defer. To me, this seems like a good trade off between making a little more work for the caller and keeping things explicit and under the caller's control.You could just make a function that creates all the mocks needed for all tests, but you don't like that. So the only option left is to specify which mocks you need for each test... which is exactly what the original code is doing.
You're running into a wall because you're trying to abstract code that's already maximally abstracted.
With the C++ destructor RAII pattern I can use it at any lexical scope I need it at, which is super handy.
it is way too easy to critique an approach that led to a problem that hasn't happened to you, and is described to you by someone else.
Instead you'd return an object, and make it the callers responsibility to close it. Destruct all three in the close call.
It may not fit your mental model, and that's fine. But let's not pretend it's a serious hurdle.
No, it's not a psychological problem. This is real complexity, and it's one of the most serious and widespread issues in software development.
The key issue is that we often have rules that say: Whenever A happens then B must happen as well. It is absolutely critical that rules like this are expressed in exactly one place and that enforcement is opt-out not opt-in.
Everything else is error prone and creates significant mental burden.
One upside of destructors is that adding destructor cleanup to a type isn't necessarily a breaking change. (In Rust terms, as long as the type isn't "Copy".) But in languages with defer or with/using statements, it is.
Another upside of destructors is that the cleaned-up state can be mostly hidden from other code. But when cleanup is a regular function, it's easier to access an object after cleanup, and you have to reason more about what happens in that case. (A Rust example here might be "What happens if you use a Box after it's freed?" The answer is that you just can't do that, even though Box doesn't have a freed/null state.)
But I get your point: with destructors you are free to change the ownership responsibilities, because the link between a variable and its releasing is implicit and carried over by the runtime, instead of explicitly being cared for by the code.
parens braces semicolons operator overloading generics etc
the lack or presence of these things are never deal breakers, it can be worth trying to adjust because maybe you will find 900 things you love about the new way that make up for the one or two things you think you can’t stand.
Sure, but it can definitely break a tie. Frankly, operator overloading can create some really nice apis. And code that's pleasing to read takes less mental overhead to pick up.
Again, the language is great. It's a young project. I'm glad you like it. Andrew seems to have a very firm idea of where Zig is headed. It's just not for me, in part because operator overloading is not a "little thing" for me.
Just one question: why he says that clojurescript is a pain in the ass?
I know he used it in multiple projects and wrote datascript library compatible.
Because you have to deal with a lot of BS for no gain over modern JS.
It's by no means impossible, but at the end of it you do wonder if the benefits of the language outweigh the hassle of getting things up and running.
A compiled language with a GC like D or Nim seems more productive..
Have written code in all the languages mentioned and the performance characteristics of them are not seriously different enough to warrant the decrease in productivity if this was the goal.
The clear usecase for Zig is in resource-constrained environments (it produces the smallest binaries I've ever seen) or when you want a very strict, Rust-esque type system.
I'm not saying you can't use it outside of that, or that it isn't a great language, but that as far as how many braincells and lines of code go in to solving a problem in Zig vs D vs Nim and very different for what is generally a small amount of performance.
(Also both of those languages have no-GC modes, just FWIW)
I'm disappointing with the author for not trying Modern C++ for himself instead trusting the "others".
I trusted the "others" (Eg Linus and Richard Kenneth Eng) and missed JavaScript and C++ for so long.
Now, I use both of them. Never trust "others". Try yourself and you will see how amazing those languages are.
You fought with borrower checker with Rust but in C++ you don't have to.
With Zig, you have to manage the memory manually but with C++ you don't have to thanks to the RAII
If you went with C++, you probably have saved several hours of your time.
Zig is cool but I am not gonna to use it near time soon unless they add overloading.
I understand they want to keep the language simple. But this same simplicity itself gonna to hurt the language in future when used in large applications.
"what you read is what you get" is what actually failed C in large applications. It's impossible to know everything in whole world. We have to learn to appreciate abstractions.