Search code examples
elixir

Understanding the Elixir Hexdocs (on the example File.Stream)


The Elixir Hexdocs describe for the module File, function stream!, that it Returns a File.Stream. From my understanding of the concept of modules in Elixir, File.Stream is a module, whose definition is nested inside the File module.

Looking up the docs for File.Stream, I don't find any information about any functions. It just says that it Defines a File.Stream struct. This seems to be the underlying data structure which conceptionally models such a 'filestream'. The docs don't list any function definitions, so I don't find it documented, how I can actually use that structure.

Of course it is easy to find examples on the Net, showing functions which work with a filestream. It even seems to me that I could use a File.Stream at any place where a Stream can be used, which would mean that I could use all the functions which would also work on a Stream.

However, this doesn't seem to be documented anywhere. I don't even see in the docs, what protocols are implemented by File.Stream. Is this implicit knowledge, perhaps based on the fact that the names File.Stream and Stream are so similar, that the Hexdocs don't seem it worthwhile to document this explicitly? Or is there anywhere an index for the Hexdocs, showing what can be done with such data structures?

With other words: How can I find in the Hexdocs what I can do with a File.Stream?


Solution

  • It is documented as part of File.stream!/3 function. Quoting:

    The stream implements both Enumerable and Collectable protocols, which means it can be used both for read and write.


    Here will be a little bit more about protocols.

    […] that the Hexdocs don't seem it worthwhile to document this explicitly? Or is there anywhere an index for the Hexdocs, showing what can be done with such data structures?

    No, the HexDocs do not document all protocols implemented by given struct as it is impossible to know in general. Elixir do not have anything that is similar to "orphan rule" from Rust, which mean that new protocols can be implemented for any struct anywhere in the system. That means that any dependency can add more protocols for that structure. Due to that it is impossible to list all protocols implemented for given struct in reliable way.