Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mgwidmann/named_args
Allows named arg style arguments in Elixir
https://github.com/mgwidmann/named_args
Last synced: about 2 months ago
JSON representation
Allows named arg style arguments in Elixir
- Host: GitHub
- URL: https://github.com/mgwidmann/named_args
- Owner: mgwidmann
- License: mit
- Created: 2016-05-20T03:29:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-31T02:17:33.000Z (almost 7 years ago)
- Last Synced: 2024-10-12T03:11:28.357Z (2 months ago)
- Language: Elixir
- Homepage:
- Size: 10.7 KB
- Stars: 28
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Allows named arg style arguments in Elixir. (Macros)
- fucking-awesome-elixir - named_args - Allows named arg style arguments in Elixir. (Macros)
- awesome-elixir - named_args - Allows named arg style arguments in Elixir. (Macros)
README
# NamedArgs
Inspired by [Named Arguments with Elixir](https://blog.praveenperera.com/named-arugments-with-default-values-in-elixir/)
Allows you to use named arguments similar to Ruby's named arguments. For example, in Ruby
```ruby
def introduction(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.
```elixir
defmodule Talk do
def introduction(opts \\ [name: "Sarah", birthday: "1985-12-30"]) do
IO.puts "Hi my name is #{opts[:name]} and I was born on #{opts[:birthday]}"
end
end
# Drops the birthday
Talk.introduction(name: "Joe") # => Hi my name is Joe and I was born on
# Drops the name
Talk.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:
```elixir
defmodule Talk do
use NamedArgs
def introduction(opts \\ [name: "Sarah", birthday: "1985-12-30"]) do
IO.puts "Hi my name is #{opts[:name]} and I was born on #{opts[:birthday]}"
end
end
# No params!
Talk.introduction # => Hi my name is Sarah and my birthday is 1985-12-30
# Keeps the birthday
Talk.introduction(name: "Joe") # => Hi my name is Joe and I was born on 1985-12-30
# Keeps the name
Talk.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
```## Installation
This package is [available in Hex](https://hex.pm/packages/named_args), to install:
1. Add named_args to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{: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!