Skip to content

System.IO.File.Close() missing; prevents writing idiomatic F# code #16656

@wyoung

Description

@wyoung

Although I realize that .NET Core is intentionally stripped down relative to the full .NET platform, this method is necessary for idiomatic (i.e. single-assignment) programming with F# on Windows. Without it, the only way to close a file is to delete the File object which requires coping with nulls, a thing F# tries very hard to avoid.

Explicitly closing a file is often necessary on Windows due to its mandatory file locking semantics. For example, you cannot typically move or a file to another directory while a File object is open for writing on that file.

Lacking a Close() method, single-assignment code such as this:

 open System.IO

 let file = File.Create("myfile", ...)
 ... do stuff with file
 file.Close()
 ... do something with myfile on disk

must be rewritten to use a mutable file object:

 open System.IO

 let mutable file = File.Create("myfile", ...)
 ... do stuff with file
 file <- null
 ... do something with myfile on disk

Although that looks like a simple fix, now all the F# code dealing with file has to check for null before using the value, a very un-F# style of coding.

(Moved from [https://github.com/dotnet/cli/issues/1776])

Metadata

Metadata

Assignees

Labels

area-System.IOquestionAnswer questions and provide assistance, not an issue with source code or documentation.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions