Specify a link to the Elixir @spec function in another @spec decoration function

Is there a way to refer to a specification for a function return type in other function specifications?

defmodule Car do
  @spec beep(none()) :: String.t
  def beep do
    "beep"
  end

  @spec beep_log(none()) :: String.t
  def beep_log do
    IO.puts "beep log"
    beep
  end
end

Is it possible to specify specifications for beep_log something like this:

 @spec beep_log(none()) :: beep()
+4
source share
1 answer

You cannot do this without defining a new data type that will return both functions. You can see an example of this pattern in the documentation for the moduleGenServer , which on_startis defined and separated start/3and start_link/3.

In your case, something like this will work:

defmodule Car do
  @type beep_return() :: String.t

  @spec beep(none()) :: beep_return()
  def beep do
    "beep"
  end

  @spec beep_log(none()) :: beep_return()
  def beep_log do
    IO.puts "beep log"
    beep
  end
end
+6
source

All Articles