Skip to content

hunterboerner/magic-elixir-macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Magic Elixir Macro

The Elixir macro found in magic.ex allows you to call functions without specifying the arguments (as long as the name matches a local variable).

A module that will turn functions annotated with @replace :true into functions with an additonal argument and then add arguments to zero arity function calls for functions that require arguments.

Example

defmodule Thing do
  use Magic

  @replace true
  def my_func() do
    user_id = :foo
    password = %{plaintext: "lol ok"}
    object = new_arg

    object = put_user_id()
    arg = :bar

    object = put_another()
  end

  defp put_user_id(object, user_id) do
    Map.put(object, :user_id, user_id)
  end

  defp put_another(object, arg) do
    Map.put(object, :arg, arg)
  end
end

The code above turns my_func/0 into

  def my_func() do
    user_id = :foo
    password = %{plaintext: "lol ok"}
    object = new_arg

    object = put_user_id(object, user_id)
    arg = :bar

    object = put_another(object, arg)
  end

Disclaimer

I am not responsible if you actually use this. Please don't use this, it was a fun exercise in writing macros.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Contributors

Languages