notexactlyawe
I can’t seem to pass an anonymous function as an option to a Plug. I am using plug_hmouse which from the documentation takes an option digest that is a function to be called on a string. The example is provided below:
plug PlugHMouse,
... # other options
digest: fn string -> Base.encode16(string) end
However when I try this in my router.ex, I get the following error:
router.ex
plug PlugHMouse,
validate: {"x-hub-signature", webhook_secret},
hash_algo: :sha,
split_digest: true,
digest: fn string -> Base.encode16(string, :lower) end
error
== Compilation error in file lib/spellcanary_web/router.ex ==
** (ArgumentError) cannot escape #Function<0.85932033 in file:lib/spellcanary_web/router.ex>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
(elixir) src/elixir_quote.erl:122: :elixir_quote.argument_error/1
(elixir) src/elixir_quote.erl:259: :elixir_quote.do_quote/3
(elixir) src/elixir_quote.erl:421: :elixir_quote.do_quote_splice/5
(plug) lib/plug/builder.ex:312: Plug.Builder.init_module_plug/4
(plug) lib/plug/builder.ex:286: anonymous fn/5 in Plug.Builder.compile/3
(elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
(plug) lib/plug/builder.ex:284: Plug.Builder.compile/3
lib/spellcanary_web/router.ex:17: (module)
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
I don’t get this error when passing &Base.encode16/1 but I do get it when using an anonymous function or when defining a function in my router and passing that, eg.
def encode16lower(string) do
Base.encode16(string, :lower)
end
...
plug PlugHMouse,
validate: {"x-hub-signature", webhook_secret},
hash_algo: :sha,
split_digest: true,
digest: &self().encode16lower/1
I need the anonymous function since I want to add an extra argument to the function I’m calling. How can I get around this error?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Module attributes can not contain anonymous functions. They may only contain a very strict subset of values. Whereas
&Mod.fun/aritis one of these.self()does not return a module name, but a PID. Probably you meant__MODULE__.encode16lower/1?notexactlyawe
Thanks NobbZ, the
__MODULE__tip fixed the error! I didn’t realise that callingplugwould create a module attribute. For anyone reading this thread in future, the docs on Module attributes actually have some info on this.budgie
I don’t really understand this. Having this same issue, not sure how to go about solving it. I want to be able to dynamically define redirect behaviour. What do you mean about the module attributes?
I have this plug:
But I can’t use it like expected:
Because I get the error
cannot escape #Function<0.92307874/1 in :elixir_compiler_158.__MODULE__/1>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity. Not quite sure what this is telling me and how I can solve it. For such simple implementations I try to keep the definition as close to the usage as possible to facilitate understanding, but not sure how I can do that if I can’t use an anonymous function.al2o3cr
The issue is that
Plugpre-compiles the stack of plugs, so theinitfunction is called at compile time and its result is stored in the compiled module.As the error message states, that place can’t store anonymous functions.
The “standard idiom” for this situation is to use a tuple to specify a function by name (you’ll see the term “MFA tuple” a lot). Something like:
If
some_funexpects more than one argument, you’d include it in the tuple’s third element.budgie
Why can’t it store anonymous functions? Also, is ther a reason to prefer an MFA tuple over referencing the named function directly as in &SomeModule.some_fun/1?
jswanner
The ability to include extra arguments
budgie
Can’t you already do that with the &SomeModule.other_fun(&1, other_arg) syntax for a function that accepts 2 arguments? Or are you meaning something else?
al2o3cr
&SomeModule.other_fun(&1, other_arg)is a shorthand forfn x -> SomeModule.other_fun(x, other_arg) endand will trigger precisely the same problems as any other anonymous function.Despite the similar notation,
&SomeModule.other_fun/1is represented differently internally.budgie
Understood, thank you!