You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allows you to use named arguments similar to Ruby's named arguments. For example, in Ruby
defintroduction(name: 'Sarah',birthday: "1985-12-30")puts"Hi my name is #{name} and I was born on #{birthday}"end
However in Elixir, using a default argument ends up dropping the other values.
defmoduleTalkdodefintroduction(opts\\[name: "Sarah",birthday: "1985-12-30"])doIO.puts"Hi my name is #{opts[:name]} and I was born on #{opts[:birthday]}"endend# Drops the birthdayTalk.introduction(name: "Joe")# => Hi my name is Joe and I was born on# Drops the nameTalk.introduction(birthday: "1985-12-30")# => Hi my name is and I was born on 1985-12-30
With NamedArgs you can instead do the following:
defmoduleTalkdouseNamedArgsdefintroduction(opts\\[name: "Sarah",birthday: "1985-12-30"])doIO.puts"Hi my name is #{opts[:name]} and I was born on #{opts[:birthday]}"endend# No params!Talk.introduction# => Hi my name is Sarah and my birthday is 1985-12-30# Keeps the birthdayTalk.introduction(name: "Joe")# => Hi my name is Joe and I was born on 1985-12-30# Keeps the nameTalk.introduction(birthday: "1986-01-01")# => Hi my name is Sarah and I was born on 1986-01-01# Order does not matter!Talk.introduction(birthday: "1986-01-01",name: "Joe")# => Hi my name is Joe and I was born on 1986-01-01
Add named_args to your list of dependencies in mix.exs:
defdepsdo[{:named_args,"~> 0.1.0"}]end
But it doesn't create a variable with the same name?
Yup, I'm still not sure if thats a desired feature or not. Because it would implicitly create a variable that you may or may not decide to use, it goes against a lot of the philosophies that Elixir follows in regard to explicit coding. And the compiler would warn you about it (and it couldn't be fixed either).
If possible to remove the compiler warning this feature may become more attractive. Please let me know if you have ideas about this!