chulkilee

chulkilee

Typespec for Enumerable

I wrapped an API using pagination with Stream.unfold so that it returns Enum.t (Enumerable.t).

Is it possible to specify @spec of values that will be yield from the Enumerable struct?

Marked As Solved

alfert

alfert

You can of course write a spec for your function, where you say what type of values are returned. But you cannot derive it from the type spec of the enumerable or the stream itself. This has two reasons: First, the type of an enumerable, Enum.t has no type parameter, which would constraint an enumerable to, say an enum of integers. Secondly, the missing strict static typing of Elixir allows for putting all kinds of values into an enumeration. That is the reason why in the API of Enumerable the type element is only an alias of any.

What you can do is to spec your function in way to say it returns a list of a specific element type, e.g. list(integer). The diaylzer can then check, whether this specification is fulfilled in your program.

Also Liked

alfert

alfert

It depends what you want to do with the type specification. If it should be a help for the reader or programmer, than you can define your enumeration type for your API, e.g. something like

@opaque my_enum(t) :: list(t) | Enum.t

This compiles, reveals no details about the internal of type (thus @opaque) and you communicate your intention. But the dialyzer cannot help at finding type errors. The following snippet is wrong, but dialyzer does not complain:

  @opaque my_enum(t) :: Enum.t | Enumerable.t | list(t)

  @spec take_ten(integer) :: my_enum(integer)
  def take_ten(max) do
    max
    |> Stream.unfold(fn 0 -> nil; n -> {n, n-1} end)
    |> Stream.take(10)
  end

  @spec ten_ints() :: list(atom)
  def ten_ints() do
    take_ten(20) |> Enum.to_list()
  end

I assume that it comes from type union. It could be a list of atoms, because the stream functions are allowed to return enumerable of any type. And you cannot use Enum.t(integer) because it is not defined in the Enum module. This could be an interesting bug report and PR for the Enum module, because you could define type t this way:

@type t :: t(any)
@type t(x) :: Enumerable.t(x)

You need to specify properly all enum functions to take parameterized types. But I am unsure how the type definition on the protocol level works, in particular since Enumerable.t is not defined explicitly but seems to generated by defprotocol.

Last Post!

alfert

alfert

It depends what you want to do with the type specification. If it should be a help for the reader or programmer, than you can define your enumeration type for your API, e.g. something like

@opaque my_enum(t) :: list(t) | Enum.t

This compiles, reveals no details about the internal of type (thus @opaque) and you communicate your intention. But the dialyzer cannot help at finding type errors. The following snippet is wrong, but dialyzer does not complain:

  @opaque my_enum(t) :: Enum.t | Enumerable.t | list(t)

  @spec take_ten(integer) :: my_enum(integer)
  def take_ten(max) do
    max
    |> Stream.unfold(fn 0 -> nil; n -> {n, n-1} end)
    |> Stream.take(10)
  end

  @spec ten_ints() :: list(atom)
  def ten_ints() do
    take_ten(20) |> Enum.to_list()
  end

I assume that it comes from type union. It could be a list of atoms, because the stream functions are allowed to return enumerable of any type. And you cannot use Enum.t(integer) because it is not defined in the Enum module. This could be an interesting bug report and PR for the Enum module, because you could define type t this way:

@type t :: t(any)
@type t(x) :: Enumerable.t(x)

You need to specify properly all enum functions to take parameterized types. But I am unsure how the type definition on the protocol level works, in particular since Enumerable.t is not defined explicitly but seems to generated by defprotocol.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement