3 ms·
>The code will compile just fine. The Zig compiler will generate a new number for each unique 'error.*' This is wild. I assume there's at least the tooling to
by aeve890 1y ago
>The code will compile just fine. The Zig compiler will generate a new number for each unique 'error.*'
This is wild. I assume there's at least the tooling to catch this kind of errors right?
- TUSF 1y agoThere's no special tooling to catch this, because nobody catches an error with if-else—it's simply not idiomatic. Everyone uses switch statements in the catch block, and I've never seen anybody using anything other than switch, when catching an error.
- veber-alex 1y agoBut why? If I just need to check for 1 specific error and do something why do I need a switch? In Rust you have both "match" (like switch) and "if let" which just pattern matches one variant but both are properly checked by the compiler to have only valid values.
- arwalk 1y agoThe real problem is not about the if-else, its that he's comparing to the global error set, and not to the FileError error set he created specifically to define AccessDenied.
- love2read 1y agoThe real problem is that this compiles without error
- love2read 1y ago“There’s no specific tooling to catch this, because nobody[…]”. So? This is a standard library/language feature, which is usually the first place people go for features in the language. To say that nobody uses it is stupid.
- dminik 1y agoBoth the Zig standard library as well as several third party projects do check errors like this. I already commented on Zig compiler/stdlib code itself, but here's Tigerbeetle and Bun, the two biggest(?) Zig codebases: https://github.com/search?q=repo%3Atigerbeetle%2Ftigerbeetle%20%22%3D%3D%20error.%22&type=code https://github.com/search?q=repo%3Atigerbeetle%2Ftigerbeetle... https://github.com/search?q=repo%3Aoven-sh%2Fbun%20%22%3D%3D%20error.%22&type=code https://github.com/search?q=repo%3Aoven-sh%2Fbun%20%22%3D%3D...
- dminik 1y agoOk, while it's cool that the TigerBeetle link now shows no matches (down from two) the comment now feels wrong. Anyways, you guys left in the `!= error.` checks, so here's some snapshots that hopefully won't be invalidated :P https://github.com/tigerbeetle/tigerbeetle/blob/b173fdc8270016e8c29da06640c726523e8acb90/src/vsr/multi_batch.zig#L490 https://github.com/tigerbeetle/tigerbeetle/blob/b173fdc82700... https://github.com/tigerbeetle/tigerbeetle/blob/b173fdc8270016e8c29da06640c726523e8acb90/src/state_machine_fuzz.zig#L104 https://github.com/tigerbeetle/tigerbeetle/blob/b173fdc82700... (different file, same check.)
- matklad 1y agoGood catch of != thanks!
- arwalk 1y agoThe example is a bit dubious. Sure, it compiles just fine, because the author is not using errors properly in zig. Here, he uses the global error set with `error. AccessDenid`, and as stated, it compiles just fine because when you reach the global error set, it's integers all the way down. If the author had written `FileError.AccessDenid`, this would not have compiled, as it would be comparing with the `FileError` error set. The global error set is pretty much never used, except when you want to allow a user to provide his own errors, so you allow the method to return `anyerror`.
- veber-alex 1y agoWhy does the compiler decay the FileError and the global error set to an integer? If they were unique types, the if statement would not have compiled.
- arwalk 1y ago`FileError.AccessDenied` is a unique value in a unique error set. `error.AccessDenid` has not been defined anywhere and hence is just given an integer value at some point by the compiler. As I stated before, this error wouldn't even exist in the first place in no codebase ever: look how the method that fails returns a `FileError` and not an `anyerror` It could be rightly argued that it still shouldn't compile though.
- empath75 1y agoAll examples of this type come down to "the user made a mistake", but that is kind of the the entire point. It is not possible to make that mistake in rust.
- arwalk 1y agoI'm not saying that zig has the same level of safety than rust, i'm just saying that grabbing a knife by the blade is not an argument for using a spoon. The error presented in this example would not be written by any zig developer. Heck, before this example i didn't even knew that you could compare directly to the global error set, and i maintain a small library. zig and rust do not have the same scope. I honestly do not think they should be compared. Zig is better compared to C, and rust is better compared to C++.
- matklad 1y agoThis is compiler bug, the code should not compile (see issue #25046)