title: Implementing a public protocol with internal class in swift
body: <p>So I'm writing a Swift framework where I want to expose some objects publicly only through a <code>protocol</code>.</p>
  
  <p>eg:</p>
  
  <pre><code>public protocol Thing : class {
      var x : Int { get }
  }
  </code></pre>
  
  <p>I want to hide my implementation details so I make an <code>internal</code> class:</p>
  
  <pre><code>internal class ThingImpl : Thing {
      public private(set) var x : Int = 0
  }
  </code></pre>
  
  <p>And vend out new <code>Things</code> via a factory method:</p>
  
  <pre><code>public func newThing() -&gt; Thing {
      return ThingImpl()
  }
  </code></pre>
  
  <p>What I can't understand is why the compiler warns me about making <code>var x</code> <code>public</code> within <code>ThingImpl</code> and encourages me to declare it internal.</p>
  
  <p><a href="https://i.stack.imgur.com/gz3Sf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gz3Sf.png" alt="enter image description here"></a></p>
  
  <p>Everything runs fine despite the warning, and if I heed the warning and change to <code>internal private(set) var x : Int = 0</code> the warning goes away and things still run fine.</p>
  
  <p>It seems to me that making the var public is the right thing to do since it is public in the public protocol it is implementing.
  Am I missing something here? Thanks.</p>