I've been thinking about how the IO interface should be organized in
perl6. It seems that part of S16 has received little attention so far.
One main problem with filehandles is that are rather diverse. The only
operation that all of them have in common is close. Reading versus
writing is a obvious difference, there are many more differences. One
can't seek a socket or a pipe, and likewise one can't accept on a
file. A listening socket can't even be read or written to. At the same
time, they all do have some parts in common with some other types of
Perl 5's solution is to use a fat interface, and raise an error if an
unsupported action is tried. I think that approach is needlessly
fragile. Another solution is to make a separate class for every
variety of handle, but IMO that's a plain fugly solution that simply
doesn't scale up very well.
What I propose is using role composition for *everything*. Most
importantly that includes the roles Readable and Writable, but also
things like Seekable, Mapable, Pollable, Statable, Ownable, Buffered
(does Readable), Socket, Acceptable (does Pollable), and more.
That may however make some interfaces is a bit wordy. I think that can
be conveyed using a subset like this (though that may be abusing the
subset File of Mapable & Pollable & Statable & Ownable;
In this case, these calls:
would return a Writable & File, and
Socket::connect($remote_host, $port, :type<inet>);
would return a Socket & Buffered & Pollable & Writable