Backporting Swift 5.9's observability

When I had a need to "back port" diffable data source machinery to older OS I used something like this:

// analogue of UITableViewDiffableDataSource for older OS versions
class MyTableViewDiffableDataSource<Section: Hashable, Item: Hashable> {
    private var realDS: AnyObject!
    
    @available(iOS 13, *)
    var ds: UITableViewDiffableDataSource<Section, Item> {
        realDS as! UITableViewDiffableDataSource<Section, Item>
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard #available(iOS 13, *) else {
            return tableView.cellForRow(at: indexPath)!
        }
        return ds.tableView(tableView, cellForRowAt: indexPath)
    }
    
    // other methods
}

Was a bit painful but possible. In principle such a wrapper (which is very lightweight) could be within the library to allow this new type on pretty much any OS version, and the wrapper's overhead (runtime / space) is tiny to be noticeable.


Alternatively could we do this like security patches are done? So the new feature is not available on, say, 14.8 or 15.7, or 16.6, but user can upgrade to 14.9 or 15.8, or 16.7 to get a new feature. Version checks with such fragmentation could be challenging though :crazy_face: