I want to preface this by saying that I’m feeling very thankful for all the helpful answers I’ve been getting from the community and feel like I’m to the point where going to the source code first is really productive.
Getting into the difference between Enumerable and Collectable protocols and how they are invoked in the Enum and Stream modules, I ran across something that didn’t make sense to me.
Setting aside the deprecated collectables (non-empty and keyword lists), I understand the following collections to implement the two different protocols:
Enumerable | Collectable (supported) |
---|---|
IO.Stream | IO.Stream |
Range | Empty List |
List | BitString |
Map | Map |
Function | MapSet |
MapSet | Mix.Shell |
DateRange | File.Stream |
Stream | Stream |
File.Stream |
I haven’t been able to reconcile the spec typing with what seems to work.
@spec into(Enumerable.t(), Collectable.t()) :: Collectable.t()
Enum.into(enumerable, collectable)
Some situations probably aren’t that helpful like passing a range into an empty map but should it work if they typing says they should?
Enum.into(1..10//2, %{})
** (FunctionClauseError) no function clause matching in anonymous fn/2 in Collectable.Map.into/1
The following arguments were given to anonymous fn/2 in Collectable.Map.into/1:
# 1
%{}
# 2
{:cont, 1}
(elixir 1.15.7) anonymous fn/2 in Collectable.Map.into/1
(elixir 1.15.7) lib/enum.ex:1554: anonymous fn/3 in Enum.reduce_into_protocol/3
(elixir 1.15.7) lib/range.ex:526: Enumerable.Range.reduce/5
(elixir 1.15.7) lib/enum.ex:1553: Enum.reduce_into_protocol/3
(elixir 1.15.7) lib/enum.ex:1537: Enum.into_protocol/2
/Users/bradhanks/membrane_tutorial.livemd#cell:wp5kyzkt6fqb75ktz3vdbl7sbfehlmzc:1: (file)
It makes sense that the Collectable.Map.into/2
expects a tuple and that Collectable.BitString.into/2
only has function clauses that match for binary or bitstring.
I’m looking for a better understanding on what the types in @spec represent.