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

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
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
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
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

We're in Beta

About us Mission Statement