glmeocci
Use a Typespec function type
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!
Most Liked Responses
zachallaun
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
al2o3cr
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
zachallaun
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
Popular in Questions
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Other popular topics
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New








