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 matching和 guard 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 returns65
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/