Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have an alias of function inside the same library in Elixir?

Tags:

elixir

Say I have the definition of a function:

def rename(src, dst) do
   <do rename>
end

inside of my Elixir library, can I then create within the same library:

alias rename, as: mv  

so that when users can use both the rename and mv functions in my library ?

like image 459
Muhammad Lukman Low Avatar asked Feb 02 '15 14:02

Muhammad Lukman Low


1 Answers

The simplest approach I can think of is via defdelegate

iex(1)> defmodule Foo do
...(1)>   def foo, do: :foo
...(1)>
...(1)>   defdelegate bar, to: __MODULE__, as: :foo
...(1)> end

iex(2)> Foo.foo
:foo
iex(3)> Foo.bar
:foo

Note that this defines another function bar/0 which invokes foo/0.

like image 72
sasajuric Avatar answered Oct 20 '22 11:10

sasajuric