gleam-lang / gleam

⭐️ A friendly language for building type-safe, scalable systems!
https://gleam.run
Apache License 2.0
17.95k stars 748 forks source link

utf16 serialization segment flag forgotten #2593

Open sdancer opened 9 months ago

sdancer commented 9 months ago
  let name = "marimota"
  let padded_name =
    bit_array.slice(
      bit_array.append(<<name:utf16>>, bit_array_copy(<<0, 0>>, 22)),
      0,
      44,
    )

produces erlang

    Name@1 = <<"marimota"/utf8>>,
    Padded_name = begin
        _pipe = gleam_stdlib:bit_array_slice(
            gleam@bit_array:append(<<Name@1/binary>>, binary:copy(<<0, 0>>, 22)),
            0,
            44
        )

added extra context just in case

lpil commented 9 months ago

Thank you.

giacomocavalieri commented 9 months ago

What should the generated code look like instead?

sdancer commented 9 months ago

<Name@1/binary-utf16> or similar also erlang doesn't automatically convert them, since on gleam we use strings rather than binaries, a good solution would be the compiler backend to do the conversion by calling unicode:characters_to_binary(Data, InEncoding, OutEncoding) similarly might want to fetch the right implementation on javascript side or at least error

giacomocavalieri commented 9 months ago

Thanks for the clarification!

lpil commented 9 months ago

I think we should error in this case, like Erlang does.

sdancer commented 9 months ago

erlang errors after trying to serialize it from utf8, to preserve that behavior can just emit the utf-16 and let erlang error