I was wondering where the single quote syntax might be useful. I have read through the String discussions and the consensus seems to be, that the Unicode will be a separate package. My question is then why is 'a' in the language. Could we switch to a more python like approach (unicode.Scalar.from "a"
or python's ord()
).
It can be used for pattern matching
startsWithS = \str ->
when Str.toUtf8 str is
['s', ..] -> Bool.true
_ -> Bool.false
that pattern breaks if you want to pattern match on Unicode strings :(
'Š' -> cant be made to U8
out of curiosity, have you run into a situation where you want to pattern match on something outside the ASCII range like 'Š'
?
Honestly, haven't pattern matched, tho words like "Živijo" are quite common in my language. I did run into a scenario, where I wanted to implement a parser. In that case I couldn't take just the first u8 and leave the rest as Str, since that would cause utf8 error.
indeed it would! :big_smile:
My point being, if 'a' isn't useful outside the unicode library, why not move it there?
The other sensible thing I would recommmend is including enough of the unicode library under Str, that it would make sense, to use single quotes.
we introduced it specifically because people wanted to parse ASCII U8
s using pattern matching
previously the only way to do that was using numbers, and then you had to separately document somehow what the actual character you wanted to parse was
so if we removed it from the language, that scenario would once again be painful and there would be immediate demand for re-adding it to the language
we've talked about giving an error if you put anything outside the ASCII range in single quotes, which would make it strictly less useful than today, but would make it more obvious what its primary motivating use case is
I see... and introducing zig's comptime would greatly complicate the language
when x is
(ord "a") -> Ok {}
_ -> Err {}
Adrian has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC