Hi,
probably I miss something but I’m trying to fix this scenario:
Imagine a simple module:
defmodule A do
@type simple_function function(integer() -> non_neg_integer())
....
end
Now imagine that in a module B
I want to “reuse” the same type spec for another function without copy & paste it. I’d like to write smtg like this:
defmodule B do
@spec my_fun :: B.simple_function()
def my_fun(x) when x >=0, do: x + 1
def my_fun(x), do: -x + 1
end
Instead of
defmodule B do
@spec my_fun(integer()) :: non_neg_integer()
def my_fun(x) when x >=0, do: x + 1
def my_fun(x), do: -x + 1
end
Thanks!
This is accomplished using behaviours:
defmodule A do
@callback simple_function(integer()) :: non_neg_integer()
end
defmodule B do
@behaviour A
@impl A
def simple_function(int), do: ...
end
I’ll add, however, that it’s not advisable to create behaviours if your goal is really only to re-use some typespecs. Two different functions sharing a common, duplicated typespec is not a code smell.
1 Like
I thought you can do this. For example, I’ve done stuff like this before…
defmodule MyThing do
@spec start_link(term) :: GenServer.server()
def start_link(arg) do
GenServer.start_link(__MODULE__, arg)
end
end
This cover the return type I guess
You can write this spec, it just doesn’t mean what you want:
defmodule B do
@spec my_fun :: A.simple_function()
def my_fun do
fn
x when x >= 0 -> x + 1
x -> -x + 1
end
end
end
1 Like
Yes, you can reference remote types for any of the arguments or the return type, but you can’t do
@spec some_function :: some_generic_function_signature()
which is what the OP was asking.
1 Like