ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - mgwidmann/named_args: Allows named arg style arguments in Elixir
Allows named arg style arguments in Elixir. Contribute to mgwidmann/named_args development by creating an account on GitHub.
Visit Site

GitHub - mgwidmann/named_args: Allows named arg style arguments in Elixir

GitHub - mgwidmann/named_args: Allows named arg style arguments in Elixir

NamedArgs

Inspired by Named Arguments with Elixir

Allows you to use named arguments similar to Ruby's named arguments. For example, in 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.

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:

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, to install:

  1. Add named_args to your list of dependencies in mix.exs:
  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!

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory