title: Swift 2: understanding AnyObject and Self
body: <p>I couldn't find any good explanation to my questions so I'd like to ask you directly. First of all I'd like to refine my code in this <a href="https://stackoverflow.com/a/31270708/4572536">post</a>.</p>
  
  <p>My problem is the protocol <code>AnyObject</code> and the <code>Self</code> type. I didn't implement <code>AnyObject</code> into my code because it is marked with <code>@objc</code> and I don't want any Objective-C stuff involved in my code (don't judge me for that). I also couldn't find any explanation about the <code>Self</code> type. It just worked as expected, but Xcode does not replace <code>Self</code> with the type the static function is called at.</p>
  
  <p>Here is some example:</p>
  
  <pre><code>extension Int : Instance {}
  
  Int.singleton { (customInstanceName) -&gt; Self in 0 } // Self shall be replaced with Int
  </code></pre>
  
  <p>As you can see Xcode produces a Self instead an Int. Is there any chance I could fix this? Am I right that Self does return the dynamicType and my implementation is fine as it is in my post above? I would really appreciate any good explanation about the <code>Self</code> type.</p>
  
  <p>As you have seen in my code. I am using a custom protocol to check whether my instance is a class or not. Is there any other shiny implementation to check my instances if they are classes or structure types, or am I forced to use <code>AnyObject</code> if I want to get rid of my ClassInstance protocol?</p>
  
  <p>Thank you for your time.</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <pre><code>protocol Test {}
  
  class A : Test {}
  
  struct B : Test {}
  
  let aClass : Test = A()
  let aStruct : Test = B()
  
  if let someClass = aClass as? AnyObject {
      print(someClass) // only this will print
  }
  
  if let someStruct = aStruct as? AnyObject {
      print(someStruct)
  }
  </code></pre>
  
  <p>This will work, but <code>AnyObject</code> is still marked as an <code>@objc</code> protocol.</p>