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
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
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









