dmarcoux

dmarcoux

Help on `?` (for getting a character's codepoint)

Hey,

I understand that ? returns a character’s codepoint, but I would like to know how ? is defined and reads its documentation. I looked in vain at a few modules in IEx and also tried h ?, but it’s not working. Where is it hiding itself?

Marked As Solved

al2o3cr

al2o3cr

The ?x syntax is handled by the tokenizer:

https://github.com/elixir-lang/elixir/blob/main/lib/elixir/src/elixir_tokenizer.erl#L233-L274

This code is also what produces the messages you get when using ? with improperly-escaped characters:

iex(1)> ? 
warning: found ? followed by code point 0x20 (space), please use ?\s instead
  iex:1:1

32

iex(2)> ?\x
warning: unknown escape sequence ?\x, use ?x instead
  iex:2:1

120

Also Liked

adamu

adamu

It is strange that it’s not possible to search for this as an operator. It almost feels like there should be an honorary mention in Kernel.SpecialForms (“the basic building blocks of Elixir”) that at least explains what’s going on.

LostKobrakai

LostKobrakai

There is not really an implementation. This is a pure syntax feature of elixir.

LostKobrakai

LostKobrakai

There’s a bunch of syntax to define numbers, which is not documented in Kernel.SpecialForms:

1234 # decimal base (the default)
0b1010101 # binary base
0o707070 # octal base
0xF0F0F0 # hexadecimal base
?e # unicode code point

None of them have AST representations. They’re turned into their respective integers before turned into AST and are therefore part of the syntax of elixir.

quote do: 0b101010
# 42
quote do: ?e
# 101

This is similar to how [abc: :def] and [{:abc, :def}] are the same in AST (though the sugar’ed form is the default representation):

quote do: [abc: :def]
# [abc: :def]
quote do: [{:abc, :def}]
# [abc: :def]

Kernel.SpecialForms on the other hand are represented in AST, but happen to be implemented directly in the compiler, because there’s no possible macro to transform their AST to some other elixir code doing what they do (or do it efficiently).

quote do: for(i <- 1..10, do: i)
# {:for, [], […]}

I can certainly see that this is confusing though. E.g. tuple and map syntax are special forms, lists are not, as lists stay lists in AST.

Last Post!

dmarcoux

dmarcoux

Cool, I’ll put the same link pinned to the latest commit SHA so others can refer to this at a later date if the file changes one day:

https://github.com/elixir-lang/elixir/blob/3b90a793fca04869cf90add0fd7144ece52f9dba/lib/elixir/src/elixir_tokenizer.erl#L233-L274

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement