Search code examples
erlangelixir

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


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.


Solution

  • 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