No tempFile() in std.file

Patrick Schluter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 16 22:30:40 PDT 2017


On Tuesday, 16 May 2017 at 13:56:57 UTC, Jonathan M Davis wrote:
> On Tuesday, May 16, 2017 11:19:14 bachmeier via 
> Digitalmars-d-learn wrote:
>> On Monday, 15 May 2017 at 22:38:15 UTC, Jonathan M Davis wrote:
>> > [...]
>>
>> That seems perfectly reasonable to me. Couldn't the function 
>> return both the path and the file in a struct? This is 
>> something that really should be in Phobos. It's one of those 
>> little things that makes D a lot less pleasurable to work 
>> with, at least for anyone needing that functionality.
>
> std.file doesn't have anything to do with File. It only 
> operates on entire files at a time, so it wouldn't make sense 
> for a function in std.file to return a std.stdio.File. At most 
> what would make sense to me would be to have a function in 
> std.file which created the file as empty and closed it and then 
> returned the file name for the program to then open or do 
> whatever else it wants with - which would actually be perfectly 
> fine if you then wanted to use std.file.write or similar to the 
> file. It's just more annoying if you want a File, because then 
> you end up effectively opening the file twice.
>
> - Jonathan M Davis

As your solution doesn't inherently solve the race condition 
associated with temporary files, you could still generate the 
name with a wrapper around tempnam() or tmpnam() (Posix for 
Windows I don't know). This would avoid the double open() of the 
scenario above.


More information about the Digitalmars-d-learn mailing list