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

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement