Unexpected token `do` in case statement

Background

I have a GenServer that on initialization tries to open an HTTP2 connection and waits for it to be up, using a library called Gun.

def init(_args), do
    {:ok, conn_pid} = Gun.open(@url_domain)

    case Gun.await_up(conn_pid) do
      {:ok, _protocol}  ->  IO.puts("Conn up!"); {:ok, conn_pid}
      {:error, reason}  ->  {:stop, reason}
    end
  end

Problem

Now, this simple code has a problem - it doesn’t compile.

unexpected token: do. In case you wanted to write a “do” expression, you must either use do-blocks or separate the keyword argument with comma. For example, you should either write:

if some_condition? do
  :this
else
  :that
end

or the equivalent construct:

if(some_condition?, do: :this, else: :that)

where “some_condition?” is the first argument and the second argument is a keyword list

May be I am brain dead right now and I don’t know how to make a case statement, but am I missing something?

Remove the comma in init(_args), do

This also looks like a good place to consider opening the connection in a handle_continue callback.

3 Likes

I am aware of the drawbacks of doing something slow (like opening a connections and waiting for it to be up) in the init function of a GenServer, but I didn’t know that continue could actually be useful here.

Do you have an example or any resources you could point me to?

Also, thanks for spotting it !

Here’s some info: https://michal.muskala.eu/post/my-otp-21-highlights/

2 Likes