Search code examples
erlangelixir-mixelixir

Miguel Camba tutorial Elixir shortcut function error


I am following the Miguel Camba tutorial on Elixir lang.

Everything is ok until I try to utilize the following snippet:

get "/play/:story_name" do
    conn = conn.resp_content_type("text/event-stream")
    conn = conn.send_chunked(200)

    iterator = File.iterator!("#{conn.params[:story_name]}.txt")

    Enum.each iterator, fn(line) ->
      { :ok, conn } = conn.chunk "data: #{line}\n"
      await conn, 1000, on_wake_up(&1, &2), on_time_out(&1)
    end
    conn
  end

  defp on_wake_up(arg1, arg2) do
    # Nothing
  end

  defp on_time_out(arg1) do
    # Nothing
  end

I have tried the following:

await conn, 1000, on_wake_up&(&1, &2), on_time_out&(&1)
await conn, 1000, on_wake_up(&(&1, &2)), on_time_out(&(&1))
await conn, 1000, on_wake_up(), on_time_out()
   end
    conn
  end

  defp on_wake_up() do
    # Nothing
  end

  defp on_time_out() do
    # Nothing
  end

I would like to run introspection to see what is the type of object passed in to (arg1, arg2) and (arg1), respectively, and still trying to figure out how to accomplish that..

In the meantime, I cannot tell whether the shortcut ampersand method is useful because I was unable to get it to work in the iex REPL. My question is how do you do troubleshoot in such case via introspection, the docs, or utilizing the shortcut ampersand method. Thanks in advance.


Solution

  • The syntax is outdated. You need to either write: &on_wake_up(&1, &2) or &on_wake_up/2. Those are anonymous functions:

    iex> is_function &is_atom/1
    true