Ash FrameworkAF
Ash Framework3y ago
16 replies
Big Hat Dorgan

Compilation error on anonymous function argument with multiple clauses

Given an extension with these definitions:
  @event_handler [
    type:
      {:spark_function_behaviour, ChannelHandler.Plugs.Handler,
       {ChannelHandler.Plugs.Handler.Function, 2}},
    required: true
  ]

  @event %Spark.Dsl.Entity{
    name: :event,
    target: Event,
    args: [:name],
    describe: """
    Handles an event matching exactly `name`.

    ## Examples

        event "create_post" do
          handler fn {payload, bindings}, socket ->
            # ...
          end
        end
    """,
    entities: [plugs: [@plug]],
    schema: [
      name: [
        type: :string,
        required: true,
        doc: """
        The event to match.
        """
      ],
      handler: @event_handler
    ]
  }


And this usage:
  handlers do
    event "create" do
      plug Plugs.CastInput

      handler fn
        {%{"id" => id}, bindings}, socket ->
          {:reply, {:ok, "Dataset #{id} created for karta #{bindings.karta.id}"}, socket}

        _, socket ->
          {:noreply, socket}
      end
    end
  end

I get this error:
== Compilation error in file lib/channels_web/channels/test_channel/dataset_handler.ex ==
** (ArgumentError) cannot inject attribute @spark_dsl_config into function/macro because cannot escape #Function<6.112779023/2 in :elixir_compiler_2.__MODULE__/1>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
    (elixir 1.14.2) lib/kernel.ex:3543: Kernel.do_at/5
    (elixir 1.14.2) expanding macro: Kernel.@/1
    /home/dorgan/dev/channels/lib/channels_web/channels/test_channel/dataset_handler.ex:1: (file)
    (spark 0.3.5) /home/dorgan/dev/channels/lib/channels_web/channels/test_channel/dataset_handler.ex:1: Spark.Dsl.__before_compile__/1


If I remove the second clause to the fn in the handler, then it works.
Was this page helpful?