Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic list of arguments in function definition #5

Closed
joaquimadraz opened this issue Feb 7, 2017 · 3 comments
Closed

Dynamic list of arguments in function definition #5

joaquimadraz opened this issue Feb 7, 2017 · 3 comments

Comments

@joaquimadraz
Copy link

Hey @danielberkompas,

I was trying define a function that includes an argument that is being destructed like this:

  def some_fn(params = d%{name, email}, person \\ Person) do
    params
  end

And I got this error a compile error: expected key-value pairs in a map, got: name.

After a while digging into the code, I found out that if I define a new macro:

  defmacro d({:%{}, context, args}, person) do

..the compile error is gone.

I'm still trying to get my head around meta programming in Elixir and this is confusing for me.
Why is destructure handling other arguments?

Thanks 😃

@ssboisen
Copy link

ssboisen commented Mar 8, 2017

Hi @joaquimadraz

You should wrap your use of d in parentheses in this case

def some_fn(params = d(%{name, email}), person \\ Person) do
  params
end

@muhifauzan
Copy link
Contributor

muhifauzan commented Mar 9, 2017

Hi @joaquimadraz, It's appear that the macro behave differently on matching operator. As @ssboisen suggest, you need to use parentheses in matching operator. Maybe someone more expert can explain why.

@joaquimadraz
Copy link
Author

@ssboisen Cool, that solves the problem. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants