Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The function name conflicts with the function in the Kernel module. Is there a way to call its own function first?

Tags:

erlang

elixir

For example, div/2 is an existing function in the Kernel module. I want to call my own div/2 function instead of the Kernel.div/2 function.

like image 381
HentaiMew Avatar asked Sep 01 '25 01:09

HentaiMew


2 Answers

You can un-import a function in Kernel by explicitly importing Kernel and excluding the functions you don't want with the except option:

defmodule A do
  import Kernel, except: [div: 2]

  def div(a, b), do: a * b

  def do_div, do: div(3, 4)
end

IO.inspect A.do_div()

Output:

12
like image 199
Dogbert Avatar answered Sep 03 '25 17:09

Dogbert


You can also call <module-name>.div/2 from inside your <module-name>

like image 28
Abhishek Kumar Avatar answered Sep 03 '25 19:09

Abhishek Kumar