title: Infer Generic Type in Class Method with Swift
body: <p>Is it possible for a generic method to infer its type based on the class in which it is being executed? I use CoreData NSManagedObject models to store and retrieve local data, and have managed to make everything generic in an easy to read and usable way, except for in one place. If a user wishes to query the local database to fetch a list of objects, he would write the following line:</p>
  
  <pre><code>let posts: [Post] = Post.all()
  </code></pre>
  
  <p>This will properly return "all" Post objects in the database, but the syntax requires that the type be defined (<code>[Post]</code>) on top of calling the method from the <code>Post</code> class itself (<code>Post.all()</code>), which feels unnecessarily redundant. Is there any way to define the generic type simply by calling the <code>all()</code> method from the <code>Post</code> class? I imagine I could just create global functions for fetching data, like so:</p>
  
  <pre><code>let posts: [Post] = all()
  </code></pre>
  
  <p>This doesn't feel nearly as readable as it would be if the syntax was as follows:</p>
  
  <pre><code>let posts = Post.all()
  </code></pre>
  
  <p>The point of trying to improve this is so that any developers who pick up this project can quickly learn the structure and style without much effort. Also, this will hopefully increase general code readability in the future, regardless of if someone is working on it or just reading it for some other reason.</p>
  
  <p>For more insight, here is a bit more information about the current structure:</p>
  
  <pre><code>//Model.swift - The model base class. All models extend this class.
  
  class Model: NSManagedObject {
      /**
      Some other stuff here
      **/
  
      //MARK: Fetch
      internal class func fetch&lt;T: Model&gt;(predicate: NSPredicate? = nil) -&gt; [T]? {
          do {
              if let request = NSFetchRequest.FromEntityName(self.entityName) { //Get entity with the name defined in the current class
                  request.predicate = predicate
                  if let result = try self.context?.executeFetchRequest(request) as? [T] {
                      return result
                  }
              }
          }
          catch let error as NSError {
              Log.Error("\(error)")
          }
          return nil
      }
  
      //MARK: Fetch general
      class func all&lt;T: Model&gt;() -&gt; [T]? {
          if let result: [T] = self.fetch() {
              return result
          }
          Log.warning("No \(self.entityName) found")
          return nil
      }
  }
  
  //Post.swift - An example model class. Extends Model.swift
  
  class Post: Model {
      //some fields
  }
  
  //Example view controller
  class ViewController: UIViewController {
      override func viewDidLoad() {
          let posts: [Post] = Post.all()
          //do stuff
      }
  }
  </code></pre>
  
  <p>If anyone has an idea about then please let me know. All help is appreciated!</p>