ArgumentNames - An easy to use and pipe friendly way to have named arugments

Hey Everyone,

I just wanted to announce a new package I have just published, argument_names.

The goal of this package is meant to test a different way of solving the “pipe to different positions” dilemma that some people are having.

The package itself is fairly straight forward, it defines a single macro defnamed that mostly works just like def.

defmodule Foo do
  require ArgumentNames
  import ArgumentNames
  
  defnamed div(a, b) do
    a / b
  end
end

Once you have your function defined (it actually becomes a macro right now), you can call it just like you would expect to be able to.

Foo.div(4, 2)
#=> 0.5

You can also use the variable names you used as arguments to refer to them in any order you want.

Foo.div(b ~> 2, a ~> 4)
#=> 0.5

You can even omit some of the names if you want, as long as you pass in the unnamed arguments in the “correct” order.

defmodule Foo do
  require ArgumentNames
  import ArgumentNames

  defnamed bar(one, two, three) do
    [one, two, three]
  end
end

Foo.foo(1, 2, 3)
#=> [1, 2, 3]

Foo.foo(2, one ~> 1, 3)
#=> [1, 2, 3]

As mentioned previously, it even works nicely with pipes.

2
|> Foo.foo(one ~> 1, three ~> 3)
#=> [1, 2, 3]

There are a couple limitations that I can think of at the moment. You cannot define functions with default parameters, nor can you define private functions. Though, these are on the roadmap if people find this a useful tool.

Give the package a go and let me know what you think!

You can find the code on sourcehut and github.

3 Likes

I just published 0.2.0 which adds support for private functions.