[swift-evolution] final + lazy + fileprivate modifiers

Joanna Carter joanna at carterconsulting.org.uk
Sat Feb 18 04:44:05 CST 2017


> While we’re bikeshedding, I’m going to add my two cents. Hold on to your hat because this might be controversial here.
> 
> I think both ‘private’ and ‘fileprivate’ are unnecessary complications that only serve to clutter the language.
> 
> It would make a lot more sense to just have internal and public only. No private, no fileprivate, no lineprivate, no protected. It’s all silly.

Removal of private in any form would prove problematic for situations where you have a "lazy" var that needs to be "reset" e.g.

////////////////
  var dataScope: DataScope = .all
  {
    didSet
    {
      if dataScope != oldValue
      {
        _fetchedResultsController = nil
      }
    }
  }
  
  private var _fetchedResultsController: NSFetchedResultsController<CDEvent>?
  
  fileprivate var fetchedResultsController: NSFetchedResultsController<CDEvent>?
  {
    get
    {
      if _fetchedResultsController == nil
      {
        // some stuff
        
        if case .filtered(let filter) = self.dataScope
        {
          let predicate = NSPredicate(format: "artist.imageName contains[cd] %@", filter)
          
          request.predicate = predicate
        }
        
        self._fetchedResultsController = NSFetchedResultsController(fetchRequest: request,
                                                               managedObjectContext: DataProvider.sharedInstance.managedObjectContext,
                                                               sectionNameKeyPath: "day.narrative",
                                                               cacheName: nil)
      }
      
      return _fetchedResultsController
    }
  }
////////////////

This code is used in a public class which is used to provide the dataSource for a UITableView, therefore, the only members that should be exposed are the relevant methods for fetching data for the cells in the tableView, which are declared in a small extension in the same file. Which is why fetchedResultsController is fileprivate for access in the extension and _fetchedResultsController is private to ensure neither I, nor anyone who takes over the code, can call the underscored version, thus failing to lazily initialise the var.

--
Joanna Carter
Carter Consulting



More information about the swift-evolution mailing list