Compilation error on anonymous function argument with multiple clauses
Given an extension with these definitions:
And this usage:
I get this error:
If I remove the second clause to the
fn
in the handler, then it works.10 Replies
I can already imagine why that’s happening. Will fix it tonight or tomorrow
enjoy your vacation :)
I see that in CodeHelpers spark only handles single clause anonymous functions, so it would be a matter of generating more clauses here https://github.com/ash-project/spark/blob/3f84eb5ac97fd61550113d00f126058a5370ad65/lib/spark/code_helpers.ex#L139-L162 I think
GitHub
spark/code_helpers.ex at 3f84eb5ac97fd61550113d00f126058a5370ad65 ·...
Tooling for building DSLs in Elixir. Contribute to ash-project/spark development by creating an account on GitHub.
Yep, exactly. We’d want to give it a name based on the hash of all clauses though, so that each definition has the same name
It’s probably a pretty simple change all things considered. I can merge a PR if you make one 😀
I'll give it a shot!
It seems we're already taking an md5 hash of the full quoted expression so I just added more clauses:
https://github.com/ash-project/spark/pull/18
GitHub
Support multiple function clauses in anonymous functions by doorgan...
This adds support for anymous functions with multiple clauses.
Before this change, this code would fail to compile:
argument fn
"foo" -> "bar"
_ -> "default"...
Looks perfect!
Actually, one small thing
And maybe not important, but I wouldn’t have expected the “unquote_splicing” to work since it would be putting commas after function defs
Can you loop over the defs and make a list of quoted expressions instead?
Actually, dunno if that will work… I guess yours is tested and works so maybe I’m getting at nothing 😂
It seems to produce the correct ast:

Let’s go with it then 👍
Merged. Will cut a release later. Thanks for the PR!
:KyouAYAYA_MM: