Syntax error about using `?` to get the Unicode code of a variable

Why do I get a syntax error when I use ? to get the Unicode code of a variable with a number in its name.

iex> a1 = "A"
"A"
iex> ?a1
** (SyntaxError) iex:: syntax error before: "1"
    |
    | ?a1
    |   ^

The ? operator allows you to get the integer code point/charcode for a UTF-8 literal character. It is resolved during parsing (before compiling) and has no concept of variables.

If you want to get the character for a hex code point then:

iex> List.to_string [0xa1]
"¡"
1 Like

Thanks for your reply. I misunderstand the usage of ?.

? is a shorthand for the integer code point of the ISO Latin-1 byte that represents a single character. The binary() type.

So when you type ?a in iex, it’s returning the code point 97, but that a is not a variable, it’s the actual character a.

?a1 is interpreted as codepoint 97 directly followed by the number 1, which is a syntax error because the compiler is expecting some kind of operator in between the codepoint and the integer 1.
e.g. ?a+1 == 98

If you put the codepoint in a list or binary, iex will “pretty print” it as its char or string representation if all bytes are representable as such

iex> ?a
97
iex> [97]
'a'
iex> <<97>>
"a"
iex> [97, 98]
'ab'
iex> [97, 198]
[97, 198]

However it falls apart when you get to characters outside the 8-bit ISO Latin-1 representation into full-on unicode

iex> ?é
233
iex> [233]
[233]
iex> <<233>>
<<233>>

Because these characters are represented as multi-byte in Unicode, to get the code points in iex you can tell IO.inspect to show you the true binary.

iex> a1 = "é"
"é"
iex> a1 |> IO.inspect(binaries: :as_binaries)
<<195, 169>>
"é"
iex> byte_size(a1)
2
iex> [195, 169]
[195, 169]
2 Likes