I was wondering if some more type inference will be built into the language.
I guess the language server provides the information?
Hovering over the code - getting a hint about what and how - we see:
const mask: u16 = 0b111000;
const lsb = @ctz(mask);
// lsb: (unknown)
In almost all cases the builtin functions provide no type inference info whatsoever.
Because they are often anytype → anytype.
Another one:
const max = 20000;
const almost_max = max - 1;
// almost_max: (max - 1) The real number would be nice to see.
What I liked in Rust (almost the only thing I liked) is that you get hints on struct sizes.
const MyStruct = struct
{
const my_size: usize = @sizeOf(MyStruct); // no useful info visible on the size.
};
Last one: is there any chance the highly abstract anytype will become more “typed”?
Now we have to dive (sometimes deeply) into the code to see what is going on.
I still feel like a zig-beginner 
There is no systemic stopping the language server from displaying this kind of information, it just hasn’t been implemented yet. Over time you will see things improve, and note that things have already improved a lot over the past years.
Generally this would probably require some kind of language support for better comptime interfaces, instead of implicitly determining valid types based on the function logic.
This has been proposed here: replace anytype · Issue #17198 · ziglang/zig · GitHub and was rejected.
Other than that it might be possible for the language server to determine some type information (like e.g. if you need certain functionality on the type), but it likely won’t work everywhere.
However if all the input parameter types are known, as in the @ctz
example you mentioned, it should be easy for the language server to figure out the result type, it’s just a matter of being able to reimplement what the compiler is doing. There is also this idea of a compiler server Implement a server in the compiler that serves information about the compilation · Issue #615 · ziglang/zig · GitHub which would allow a language server to directly query this information from the compiler, without the need to reimplement its own comptime interpreter.
2 Likes
I think relying on language servers to cover this corner of the language is unsatisfying. Sometimes you don’t have access to a language server (like when browsing github) and we don’t yet know when or if language servers will enable this kind of analysis.
To me implementing structural subtyping a lá Pythons Protocols to give some hints in function signatures feels like such a no-brainer to me, but I never felt I fully understood the many discussions I read about some kind of interface in Zig.
4 Likes
True. That is one of my triggers… Code should ideally be understandable reading it as raw text. In general I do not like type inference.
1 Like