phobos: What type to use instead of File when doing I/O on streams?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 8 15:50:51 PST 2015


On Sunday, 8 November 2015 at 20:47:08 UTC, J.Frank wrote:
> So, assuming my program reads from stdin and is supposed to 
> work on a file as well as on a pipe (which is not seekable) - 
> how can I make the compiler bark when I accidently use 
> stdin.seek()?

You don't, in general. stdin is sometimes seekable and the 
compiler doesn't know if it is or not until you try at runtime.

You could write a wrapper struct for File though that @disables 
the seek function. It would have a File member, alias file this;, 
a constructor that forwards to it (optionally, you could also 
construct it as a File and assign it), and then a @disabled 
function with the same signature as the seek call in File. Then, 
it will no longer compile as long as you use your wrapper.


More information about the Digitalmars-d-learn mailing list