tfwright

tfwright

Posting this in discussions because it’s more of an open question about API design/conventions, but while looking through the documentation around streams, I found myself wondering why File includes a stream! function whereas IO has stream. According to specs, both functions appear to work similarly, returning an Enumerable directly (not wrapped in a tuple), and neither mentions any error cases I noticed, so why the different name?

My impression was that there is a pretty strong convention that a ! suffix in a function should always indicate that an error could be expected in certain conditions, whereas a function without ! should never be expected to return an error, but could return either a “status tuple” (actually not sure if there’s a more technical term for {:ok, res}/{:error, res} return values) or a result more directly, which I guess would mean that IO.stream is more “correct,” since, unlike File.read!, File.stream! will not error even when the path is invalid, etc?

Mostly just curious because I am designing a stream API for one of my own modules where most of the functions return status couples, but I don’t foresee any error cases for the streaming API. Appreciate any input!

First 2 of 2 Posts Switch mode

al2o3cr

al2o3cr

File.stream! has a line that’s easy to miss in the docs:

Operating the stream can fail on open for the same reasons as File.open!/2

“Operating” is the key part - you can File.stream! things that don’t exist, and you won’t get the exception until something forces the stream.

For instance:

iex(1)> File.stream!("nosuchfile.txt")

%File.Stream{
  line_or_bytes: :line,
  modes: [:raw, :read_ahead, :binary],
  path: "nosuchfile.txt",
  raw: true
}

iex(2)> File.stream!("nosuchfile.txt") |> Enum.to_list()

** (File.Error) could not stream "nosuchfile.txt": no such file or directory
    (elixir 1.13.0) lib/file/stream.ex:83: anonymous fn/3 in Enumerable.File.Stream.reduce/3
    (elixir 1.13.0) lib/stream.ex:1517: anonymous fn/5 in Stream.resource/3
    (elixir 1.13.0) lib/enum.ex:4143: Enum.reverse/1
    (elixir 1.13.0) lib/enum.ex:3488: Enum.to_list/1

IO.stream can’t fail in any of these ways since it takes either a PID (that you’d get from calling File.open! and possibly getting an exception) or an atom (to refer to stdio), so it doesn’t get a !.

tfwright

tfwright OP

So I didn’t notice that actual line but I’m certainly familiar with the behavior that a File stream will fail when you try to iterate on it. I am less familiar with working with IO streams though so that was probably a bad assumption of mine that there were some cases that might cause an error at that point. I guess it’s just that lazy evaluation is an interesting edge case for this convention. My first reaction is that even if using the stream might create an error at a certain point, the function name should only express the possible return values, but it’s clear why that would be difficult to adhere to in this case.

Interestingly, my case involves different stream sources so it complicates even further. if a stream API might return an IO stream or a File stream, how should it be named? stream! because in certain conditions an error might be raised on iteration? :thinking:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement