bigmachine-io / taking-off

93 stars 27 forks source link

Passing maps as function argument #4

Open alienfluid opened 7 years ago

alienfluid commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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