-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: Add String to the type system #7734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
…ting (see ziglang/zig#7734 for more info) * Add parsing of fully qualified field, dunno if I will make it work
Just to clarify, are you proposing that "string literals", from
See #7675 / #7676 for an alternative solution. |
if UTF-8 were used, maybe string operations are UTF-8 validated for debug/release-safe build modes |
I don't think zig should have a builtin "string" type. It's way too complex to be included in the language spec (because supporting unicode isn't as trivial as it sounds and makes the language itself way more complex and requires constant updates). Having some dedicated
I think all these answers should be left to a 3rd party library and should not be bound to the language or stdlib spec |
I also disagree with having a type dedicated to strings. To keep the language simple and comprehensive, I think the privilege of being a primitive type should be reserved for things that either absolutely need it (like ints, vectors, async stack frames) or benefit from it extremely well and frequently during the vast majority of projects (like slices, optionals, errors). Strings can be easily made in userland, so they don't require a primitive type. Their benefits from becoming primitive types also aren't that vast, and they are only really useful for communication with humans, so the benefits of having primitive string types are limited. You also make the point that having primitive string types would seem familiar to newcomers from other languages. I don't like the argument that something should be done just because it's familiar, because I don't think that that's enough to warrant sacrificing progress. When I started using zig, I was actually glad that they didn't have a type for strings exactly because it showed me that this new language had its own thoughts on this sort of thing, and I liked that. If you're worried that newcomers will get confused by the lack of built-in string support, what we need is better tutorials. Your other two points can both be summarized as "being able to tell strings and byte arrays appart". For other uses, we already do have primitive types that exist simply to keep us from accidentally passing one type as another (such as aligned pointers). However, for strings, this issue can be solved in userland by making a struct. Even disregarding that, strings aren't both universally and frequently used, so solving this issue for strings alone wouldn't be worth the increase in complexity. |
I, for once, don't want to have string literals enforce valid UTF-8 encoding like Rust does, for example. As soon you start interacting with non-utf8 encodings, everything will be way more complex than it needs to be, and people will just start using A string literal should not be valid UTF-8 either. As soon as we enforce UTF-8 on people's in-language string encodings, we force to make embedded people to do way more work to get a non-standard encoded text to be processed by string functions |
Something similar to this has been rejected before in #234. |
Strings in libraries are always second-class citizens. After 30 years it is still painful to use them seamlessly in C++. Strings, at least the way most people think of them, require hidden allocation for implementation. That seems to violate one of the core precepts of Zig. Here are the options I see:
I prefer Zig's existing method of handling strings with one proviso: as noted in 1 above, it would be really nice to be able to use the type system in order to tell comptime code (and possibly debuggers) that something is printable. I was proposing a special |
We won't have a string type in the language. OP problem can be solved with a better std.fmt.format API which better communicates intent. This is a deficiency of the std lib, not the language. |
Learning the language now, and I spent 2 days and still don't know how to print a literal value from a structure on log info. Too many type errors, to something that should be simple. Even GitHub Copilot don't know how to show this. Really needs some changes... |
please ask for help in a Community and we'll be happy to assist you :) |
Hi, I was curious about string type proposals in Zig and came across this thread. I'm quite interested in the language for use in trading systems, which has predominantly used C++ for latency critical code. I do agree having a strict String type is not the way to go, but I think offering something to help with strings in the std could be useful. In my field, dev teams often are hybrid with systems developers and quants who still need to be productive but are not expected to have the same degree of developer skills. Given that, it could be tough for some who will be using Zig to be productive with strings. Have there been any discussions to offer types within the std like a string_view that would just reference the slice? I think that aligns with Zig's stance on allocations, but provides some nice functionality without the need for a dependency. Edit: I do see some string_view like stuff in the std, but wondering if this is intended to be expanded. |
Problem
Since the change in PR #6870, now everytime we need to format a string, we need to specify {s} as a format specifier.
In my project https://github.com/mlarouche/stringtime/, my
print
method looked like this:Now I need to do this:
This is far too many checks to shovel to the end user to know if the current type is a string.
Event
std.fmt.formatType
has to do this charade to know if the type is a string: Array, Pointer-To-One-Array, Pointer-To-Many, Pointer-To-C, Pointer-To-Slice and if the child type is u8.The problem that #6870 fixed would have not occurred if the type system has a proper string type in the first place.
Advantages of having a proper string type in the type system
Consider a new user that try formatting for the first time.
and see each value on an array printed instead of his string? It would be a really bad first impression of the language.
The text was updated successfully, but these errors were encountered: