Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parameterized enumerable type in elixir typespecs

Tags:

types

elixir

Is it possible to parametrize the Enumerable.t type in elixir?

so currently I have a function that takes a list of foos:

@spec the_awesome([foo]) :: any
def the awesome(foos) do
  Enum.reduce(foos, &(bar(&2, &1)))
end

and really it does not have to be a list! since the only function call is from the Enum module I'd like to change the typespec to take any Enumerable, but keep the requirement that the Enumerable must consist entirely of foos

Something like

@spec the_awesome(Enumerable.t(foo)) :: any

is this possible?

like image 267
jisaacstone Avatar asked Mar 04 '15 07:03

jisaacstone


People also ask

What is @spec in Elixir?

For this purpose Elixir has @spec annotation to describe the specification of a function that will be checked by compiler. However in some cases specification is going to be quite big and complicated. If you would like to reduce complexity, you want to introduce a custom type definition.

What is a TypeSpec?

TypeSpec provides metadata describing an object accepted or returned by TensorFlow APIs. Concrete subclasses, such as tf. TensorSpec and tf. RaggedTensorSpec , are used to describe different value types.


1 Answers

Unfortunately not right now. We would need to teach dialyzer how to handle protocols if we really want them to be expressive and there are no plans for doing such.

like image 140
José Valim Avatar answered Oct 11 '22 13:10

José Valim