← Back to context

Comment by solid_fuel

1 day ago

I love Elixir and Phoenix, but Phoenix especially uses a lot of compile-time macros and it can be a steep learning curve when you need to pull apart the skeleton framework to figure out how things are actually wired.

I pretty frequently find myself needing to open up the source to understand what's actually going on, the docs aren't bad but it often feels like they assume a lot of existing familiarity with phoenix.

In this example, `socket` is a compile time macro and it's being called with

    path = "/ws/:user_id"
    module = MyApp.UserSocket
    args = [
      websocket: [
        path: "/project/:project_id"
      ]
    ]

and what is does is register that data with the `phoenix_sockets` attribute inside the module you called `socket` from. At compile time that gets turned into a lookup inside your module, and presumable then the UserSocket module is invoked when a websocket request hits the specified path.

Would you find it more clear if socket was called like this?

    socket("/ws/:user_id", MyApp.UserSocket, [websocket: [path: "/project/:project_id"]])

Or, alternatively, would it help if the endpoint was more specifically defined like

    defmodule MyApp.Endpoint do
      use Phoenix.Endpoint, 
        otp_app: :my_app,
        web_sockets: [
          socket("/ws/:user_id", MyApp.UserSocket, [websocket: [path: "/project/:project_id"]])
        ]
    end

I think the lack of parentheses is whats throwing me off regularly with Elixir.

  • I find the optional parentheses, and the way that keyword lists are defined to be the two biggest stumbling blocks when I come back to Elixir after a while way.

    Coming from other languages, I find that

        example("with", 3, extra: "arguments", as: "a", keyword: "list")
    

    being equivalent to

        example("with", 3, [extra: "arguments", as: "a", keyword: "list"])
    

    and

        example "with", 3, extra: "arguments", as: "a", keyword: "list"
    

    always takes some extra mental effort to get through, especially when there's no parenthesis. But I appreciate not having to write all the extra brackets and parens when I get going, so I think it's a fair tradeoff.

    • Elixir has enough syntax sugar to cause diabetes.

      Personally, I like the flexibility, but yes there are a lot of rules to keep in mind.

    • one more ;)

          example("with", 3, [{:extra, "arguments"}, {:as, "a"}, {:keyword, "list"}])
      
          iex> [{:extra, "arguments"}, {:as, "a"}, {:keyword, "list"}] = [extra: "arguments", as: "a", keyword: "list"]
          [extra: "arguments", as: "a", keyword: "list"]