I/O extensions for common tasks

Andrew Pennebaker andrew.pennebaker at gmail.com
Mon Dec 10 01:59:58 UTC 2018


On Monday, 10 December 2018 at 01:51:56 UTC, Andrew Pennebaker 
wrote:
> The stdlib has all the low-level components we need to do lots 
> of different workflows. However, there are a few gaps in the 
> API in terms of high level, common tasks. For example, the API 
> can read an entire Unicode text file to a string for a given 
> filename, but not for a given File object!
>
> For now, I am accomplishing this with:
>
> // Convenience function for reading an entire UTF-8 file.
> string readUTF8(File f) {
>     string s;
>
>     foreach(ubyte[] buf; f.byChunk(1024)) {
>         s ~= buf;
>     }
>
>     return s;
> }
>
> Maybe not the best code, not much error handling and whatnot. 
> Could we add something like this (but better code), to the 
> standard lib?
>
> Another little gap occurs when passing environment variables to 
> process constructors, e.g. pipedProcess: There is no enum 
> defined to represent the default case / NO redirects. For now, 
> I am copying the raw source code here, using:
>
> cast(Redirect)7
>
> as mentioned on the pipeProcess page.
>
> https://dlang.org/library/std/process/pipe_process.html
>
> This is risky, as this magic value could change over time as 
> the API evolves. Could we declare a stable enum name for the 
> default case?
>
> Another API improvement for process creation, that could be 
> done independently of this enum work, would be to add keyword 
> argument defaults, e.g. for pipeProcess(). I think this could 
> be done without breaking backwards compatilbility. And it gives 
> the user another way to set things like environment variables, 
> while continuing to use the default values for the other 
> fields. What do you think?

Update: Looks like cast(Redirect) 7 is the default, 
cast(Redirect) 0 is being interpreted as NO redirects. Would be 
good to use enums for both of these!


More information about the Digitalmars-d mailing list