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

Passing maps as function argument #4

Open
alienfluid opened this issue Feb 21, 2017 · 3 comments
Open

Passing maps as function argument #4

alienfluid opened this issue Feb 21, 2017 · 3 comments

Comments

@alienfluid
Copy link

Just started working through this book and it's been great so far!

A quick question on this example though --

  def to_light_seconds({:meters, meters} = val, precision: precision)  do
    (meters * 3.335638620368e-9) |> round_up
  end

What is val (in the functional argument) used for? When I run the tests, I get this warning:

warning: variable "val" is unused
  lib/physics/rocketry.ex:47

What is the point of val in this example? Also, is val bound to the map that is passed as argument?

Thanks!

@alienfluid
Copy link
Author

So i just read the only other issue that it's about the same val :). I've also been using the wrong term - map instead of tuple. Lots to learn.

In the comment on that issue, it is mentioned that {:inches, inches} = val will pattern match such that the second value in val will be bound to inches as long as :inches is the first element. Makes sense, but then why is Elixir giving me a warning?

@clearjs
Copy link

clearjs commented Mar 21, 2017

val is the same as the tuple on the left. It is unused, so redundant here. But it could be useful if the function needed the whole tuple, not only its part, e.g., to pass it to another function:

def discount({:order_item, amount} = item, user) do
  amount * discount_rate(item, user)
end

@clearjs
Copy link

clearjs commented Mar 21, 2017

It's safe to remove = val, in order to make the warning disappear.

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

2 participants