ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.88k stars 2.55k forks source link

langref: exemplify practical difference between `!T` and `anyerror!T` #21454

Open Des-Nerger opened 1 month ago

Des-Nerger commented 1 month ago

Since anytype in Zig means type inference, people may be misled into thinking that anyerror also does mean type inference; but just for error types. And so, they may get an idea that !T is just an abbreviated and nicer way to write the "frequently used" anyerror!T combination.

I'm not suggesting changing the language itself here, but at least the reference should grab an attention to the distinction, give an example of where one of them could accomplish more than another, etc.

Rexicon226 commented 1 month ago

related: #5893

nektro commented 1 month ago

have you noticed this confusion happen in the wild? not saying we shouldnt add this but my initial reaction was that its unlikely given that anytype is the only any thats not a discrete type/value

mnemnion commented 1 month ago

Debatable whether anyopaque is discrete, seems more like anytype than not to me, but that's a tangent here. I agree that it's relatively unlikely that someone would think that !T could be replaced with anyerror!T without changing the meaning/type of the expression.

I wouldn't describe an anytype as type-inferred, I would describe it as type-generic. The anyerror documentation and that for inferred error sets seem pretty clear to me, is there something specific you feel is missing?