glmeocci

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

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.

al2o3cr

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
zachallaun

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.

Where Next?

Popular in Questions Top

RisingFromAshes
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
openscript
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
bsollish-terakeet
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
aalberti333
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
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
dblack
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 Top

Qqwy
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...
3271 130579 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
axelson
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...
239 49134 226
New
saif
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
AngeloChecked
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
romenigld
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

We're in Beta

About us Mission Statement