unicode - "?"运算符在 Elixir 中做什么?

标签 unicode elixir operators codepoint

Ecto source code使用表达式 ?0 , ?1等等。你可以看看他们是如何评价的:

iex(14)> ?0
48
iex(15)> ?1
49
iex(16)> ?2
50

这是什么意思?这是非常难寻找的。 ?<character> 是什么意思?实际上吗?

最佳答案

来自:https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#unicode-and-code-points

In Elixir you can use a ? in front of a character literal to reveal its code point:

如果您不熟悉code points :

Unicode organizes all of the characters in its repertoire into code charts, and each character is given a unique numerical index. This numerical index is known as a Code Point.

?<character>也可以以有趣的方式使用 pattern matchingguard clauses .

  defp parse_unsigned(<<digit, rest::binary>>) when digit in ?0..?9,
    do: parse_unsigned(rest, false, false, <<digit>>)

  ...

  defp parse_unsigned(<<?., digit, rest::binary>>, false, false, acc) when digit in ?0..?9,
    do: parse_unsigned(rest, true, false, <<acc::binary, ?., digit>>)

Elixir docs on it还澄清这只是语法。正如@sabiwara 指出的:

Those constructs exist only at the syntax level. quote do: ?A just returns 65 and doesn't show any ? operator

正如 @Everett 在评论中指出的那样,有一个有用的软件包,名为 Xray 它提供了一些方便的实用函数来帮助了解正在发生的事情。

例如 Xray.codepoint(some_char) 能做什么?<char>确实如此,但它适用于变量,而 ?仅适用于文字。 Xray.codepoints(some_string) 将执行整个字符串。

关于unicode - "?"运算符在 Elixir 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71769514/

相关文章:

linux - 使用点空格与点斜线执行文件

c# - 一个语句怎么可能同时有 = 和 ==?

c++ - 无法识别 .h 文件中定义的 typedef 类型

javascript - javascript 字符串中带有转义 unicode 字符的字符串和非转义 unicode 字符之间有什么区别?

swift - 从 NSMutableString 中删除 unicode 符号

angular - 如何将查询字符串转换为希伯来字母

html - Safari 上的方形元素符号点?

concurrency - 组合 Task.async_stream 与 Continuation 传递

elixir - iex.iexs 文件未在测试环境中加载

Elixir Phoenix全局变量插件