4 ms·
The 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 `erro
by arwalk 1y ago
The 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++.
- love2read 1y agocomparing these examples to “grabbing a knife by the blade” equates writing code that looks right to picking up a sharp and jagged object with your hands that would obviously hurt and will hurt once you hold it. It’s more like picking up a fork and being surprised to find out that it’s burning hot without any visible difference.
- NobodyNada 1y agoRust and Zig are the two most prominent new systems programming languages. It's only natural for people to compare them, from perspectives such as "I'm starting a project and need to choose what language to use". The languages are very different in scope, scale, and design goals; yes. That means there's tradeoffs that might make one language or the other more suitable for a particular person or project, and that means it can be interesting and worthwhile to talk about those tradeoffs. In particular, Rust's top priority is program correctness -- the language tries hard not to let you write "you're holding it wrong" bugs, whereas Zig tends to choose simplicity and explicitness instead. That difference in design goals is the whole point of the article, not a reason to dismiss it.
- arwalk 1y agoSure. Did i dismiss the article? Or was I only saying that the example was bad?
- NobodyNada 1y agoI was specifically responding to the statement "Zig and rust do not have the same scope. I honestly do not think they should be compared". I don't know enough Zig to have a qualified opinion on the particular example (besides being very surprised it compiled). However, I thought this post from the front page the other day had more practical and thoughtful examples of this kind of thing: https://www.openmymind.net/Im-Too-Dumb-For-Zigs-New-IO-Interface/ https://www.openmymind.net/Im-Too-Dumb-For-Zigs-New-IO-Inter...
- jibal 1y ago> The error presented in this example would not be written by any zig developer. No True Scotsman fallacy. It was written by the Zig developer who wrote it.
- dminik 1y agoYou say never, but even the Zig stdlib does this occasionally. Like here in `std/tar.zig`: https://github.com/ziglang/zig/blob/50edad37ba745502174e49af922b179b1efdd99c/lib/std/tar.zig#L642 https://github.com/ziglang/zig/blob/50edad37ba745502174e49af... Or here in `main.zig`: https://github.com/ziglang/zig/blob/50edad37ba745502174e49af922b179b1efdd99c/src/main.zig#L7631 https://github.com/ziglang/zig/blob/50edad37ba745502174e49af... And in a bunch of other places: https://github.com/search?q=repo%3Aziglang%2Fzig+%22%3D%3D+error.%22&type=code&p=1 https://github.com/search?q=repo%3Aziglang%2Fzig+%22%3D%3D+e...