Stream: beginners

Topic: ✔ How to use 'a' syntax


view this post on Zulip Adrian (Mar 07 2024 at 12:44):

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()).

view this post on Zulip Alex Nuttall (Mar 07 2024 at 14:02):

It can be used for pattern matching

startsWithS = \str ->
    when Str.toUtf8 str is
        ['s', ..] -> Bool.true
        _ -> Bool.false

view this post on Zulip Adrian (Mar 07 2024 at 14:31):

that pattern breaks if you want to pattern match on Unicode strings :(
'Š' -> cant be made to U8

view this post on Zulip Richard Feldman (Mar 07 2024 at 14:58):

out of curiosity, have you run into a situation where you want to pattern match on something outside the ASCII range like 'Š'?

view this post on Zulip Adrian (Mar 07 2024 at 15:22):

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.

view this post on Zulip Richard Feldman (Mar 07 2024 at 15:56):

indeed it would! :big_smile:

view this post on Zulip Adrian (Mar 07 2024 at 16:42):

My point being, if 'a' isn't useful outside the unicode library, why not move it there?

view this post on Zulip Adrian (Mar 07 2024 at 16:45):

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.

view this post on Zulip Richard Feldman (Mar 07 2024 at 16:47):

we introduced it specifically because people wanted to parse ASCII U8s using pattern matching

view this post on Zulip Richard Feldman (Mar 07 2024 at 16:48):

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

view this post on Zulip Richard Feldman (Mar 07 2024 at 16:49):

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

view this post on Zulip Richard Feldman (Mar 07 2024 at 16:50):

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

view this post on Zulip Adrian (Mar 07 2024 at 16:51):

I see... and introducing zig's comptime would greatly complicate the language

when x is
    (ord "a") -> Ok {}
     _ -> Err {}

view this post on Zulip Notification Bot (Mar 07 2024 at 16:55):

Adrian has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC