Open mikpe opened 1 year ago
I suppose I could also implement the I/O protocol myself, for the non-file inputs. (Not having implemented that before it didn't enter my mind as an option.)
Returning an I/O device would mean wrapping it behind a process while one may not be needed. I wonder if it makes more sense to allow the I/O module to receive "file devices" and define an official specification/behaviour for them?
I've now reimplemented my "string input" method as a proper IoDev, via a process that implements a subset of the Erlang I/O protocol.
Good points:
io:get_chars/3
Bad points:
So my wish-list would be:
gen_io
could help people with the boilerplate and providing a good structure for implementations
Is your feature request related to a problem? Please describe. I have some code which reads and processes Unicode (it's a lexical analyzer for a programming language). Input may come from a regular file or from a
string()
.file:read/2
doesn't support reading general Unicode text, so for file data I useio:get_chars/3
.When reading from a
string()
it would be beneficial to be able tofile:open/2
it as a "RAM file" and use that handle withio:get_chars/3
. Unfortunately, in this casefile:open/2
returns anfd()
instead of a generalio_device()
, which prevents using theio
module on it. Currently I work around this by plugging in my own "IoDev" like abstraction which forstring()
input handles reads and updating input position, but for file data just passes through to thefile
andio
modules.If RAM files could be opened as proper
io_device()
s then I would't need this kludge.Describe the solution you'd like Change
file:open(_, [ram | _)
to return anio_device()
. Requiring an explicit option for that, to avoid tripping up unsuspecting legacy code, would be Ok.Describe alternatives you've considered Simulating an IoDev-like object as described above works Ok but adds complexity and redundancy. Writing the
string()
to a temporary file would work but is too ugly.Additional context n/a