How do I recursively call a macro?

You cannot call contains because then you are assuming contains will be imported into the user context. You should use the fully qualified name as well as make sure you call unquote(tail):

ListAssertions.contains(tail, match_expression)

Finally, there are two other issues with your macro. First, match_expression is unquoted multiple times, which means that it will be executed multiple times. For example, if you do contains([:error, :error, :error], IO.inspect(:ok)), you should see :ok printed 3 times, which is unexpected. Finally, there is no reason to use try/rescue. Instead, you can use case and match on the head.

3 Likes