I read some of the other string proposals and find myself agreeing, that there are too many design decisions to be made for zig to have a standard String impl (like Rust for instance) that works in all use cases.
However i find myself writing a lot of comptime code (msgpack rpc, zigs formatting, etc.) that just needs to distinguish between a slice of bytes and text.
I think just a single typedef like this, would go a long way:
const utf8 = enum(u8) { _ };
although we should consider if we need a separate type for ascii. (This would make string literals really complicated though, unless we allow implicitly casting these)
The problems:
- string literals would need to change
- this would probably be a breaking change for all programs
- the compiler would be aware of this typedef?
- or it gets its own place in
std.builtin.Type?- this maybe would allow implicitly casting ascii => utf8 => u8
- or it gets its own place in
std.asciiandstd.unicodewould need to use the new type(s)- you canât just use strings in places that expect bytes
(though this proposal has the same memory layout, so you can just cast)
This would allow:
- not needing
{s}in format strings
(i always seem to forget thesmodifier) std.fmtto make text readable even if it is nested somewhere in the value
(i find this always annoys me, but not enough to make writing a custom formatter worthwhile)- any other comptime code to have this information
- for example here are the places where i wanted this:
- msgpack rpc
- js interop (string vs. ArrayBuffer)
- gui debug inspector
- for example here are the places where i wanted this: