title: Making a computed property customizable through initialization
body: <p>I have this class:</p>
  
  <pre><code>class ListView: UIView {
  
      var labels: [UnderlineLabel]
  
      var listItems: [String] {   return []   }
      var areUnselectable: Bool {   return false   }
  
  ...
  }
  </code></pre>
  
  <p>I've been using this class up until now by subclassing it and overriding it's computed properties. But now I'm in a situation where I need to use this class, but the only change I need to make is to the <code>listItems</code> property (which provides the list of strings, that are turned into <code>UnderlineLabels</code>, and can be selected (underlined)). So I'm figuring that rather than subclass <code>ListView</code> for such a trivial change, and for a single use-case, it'd be better to refactor the class to enable use by composition.</p>
  
  <p>I've only been able to come up with one solution that allows me to preserve computed property semantics (<code>listItems</code> instead of <code>getListItems()</code>) and offer customizability to subclasses via overriding, and to instantiators through initializing:</p>
  
  <ol>
  <li><p>Create an additional stored property, perhaps <code>private let getListItems: (() -&gt; [String])?</code></p></li>
  <li><p>Return the result of that, or <code>[]</code> if <code>nil</code>, by default in <code>listItems</code></p></li>
  <li><p>Allow <code>getListItems</code> to be initialized by passing a custom value in <code>init</code></p></li>
  </ol>
  
  <p>Is this the only way to get the capability I want? And if not, is there a more definitive way of doing this?</p>
  
  <p>Keep in mind that the reason <code>listItems</code> is a computed property is to give the list the ability to change during execution.</p>