[swift-evolution] Pitch: Deinit for structs

Charles Srstka cocoadev at charlessoft.com
Wed Dec 23 15:07:30 CST 2015


Introduction:

This is a rather simple proposal, requesting that structs allow the ‘deinit’ destructor, as classes currently do.

Motivation:

Sometimes it is necessary to create an object that wraps a network connection (that needs to be closed when the object is destroyed), or that writes to a file on the disk (that has to be closed when the object is destroyed). Often these objects will implement a close() method, but in the case that the user forgets to call this before the object is disposed of, it is good to call it in deinit just in case. However, deinit currently is available only to classes and not to structs. This means that value types currently cannot be used for network and file-backed constructs. Given Swift’s emphasis on value types, it seems that it would be good to add this functionality to value types.

Proposed Solution:

Allow the deinit destructor in structs.

Impact on Existing Code:

Should be no impact on existing code.

Charles



More information about the swift-evolution mailing list