Skip to content

allow unicode characters in character literals #2097

Description

@andrewrk

While solving #2088 I am about to push a change that makes this test pass:

test "unicode escape in character literal" {
    var a: u24 = '\U01f4a9';
    expect(a == 128169);
}

This makes sense since character literals are just comptime_int. There's no footgun here because you can't accidentally misuse it:

test "aoeu" {
    var str = "hello";
    str[1] = '\U01f4a9';
}
/home/andy/dev/zig/build/test.zig:5:14: error: integer value 128169 cannot be implicitly casted to type 'u8'
    str[1] = '\U01f4a9';
             ^

With that in mind, I think it makes sense to allow utf-8 characters in character literals, since we have UTF-8 source encoding. I propose this test should pass:

const std = @import("std");

test "utf8 character literal" {
    const x = '💩';
    std.testing.expect(x == 128169);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions