Where is the source code for macros in Kernel.SpecialForms?

I’m curious how the with macro is written, so I opened the Kernel.SpecialForms module here: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/special_forms.ex This appears to have all the documentation for the SpecialForms macros, but no the implementation. with looks like this in the module:

defmacro with(args), do: error!([args])

The error!/1 macro has this in it "Elixir's special forms are expanded by the compiler and must not be invoked directly". So I understand what’s happening (I think!). This stop with from being called like Kernel.SpecialForms.with ..., and the macro expansion seems to happen differently, but I’m still curious of the implementation, and I can’t seem to find this macro defined anywhere else. What’s happening here? Why is this macro different? Where’s the actual source code of with?

1 Like

Everything in Kernel.SpecialForms is in there, because it actually isn’t a macro at all. They’re documented as such to come up in documentation, but their code is dealt with directly in the compiler. You’d need to look into the erlang code of elixir to see their actual implementations. Everything in that module being there also means you cannot replicate their functionality with plain macros to some degree.

3 Likes

That makes sense. Thanks. Found it! elixir/elixir_clauses.erl at d8f1a5d6b653c14ae44c6eacdbc8e9df7006d284 · elixir-lang/elixir · GitHub

2 Likes