Having read SE-0088 and skimmed through this discussion, it's not clear to me that this Self
would support what I'm really hoping for, which is the ability to define a method on a base class (or an extension of a base class) that can return an instance of a derived class and take advantage of Swift’s type inference in the process (example here):
extension
NSManagedObject
{
class func
all(inMOC: NSManagedObjectContext,
predicateFormat inPredicateFormat: String,
_ inArgs: CVarArg...)
throws
-> [Self]
{
let entity = self.entity(inMOC: inMOC)
let req = NSFetchRequest<Self>()
req.entity = entity
let pred = NSPredicate(format: inPredicateFormat, argumentArray: inArgs)
req.predicate = pred
return try inMOC.fetch(req) as [Self]
}
}
class MyEntity : NSManagedObject {}
let myEntities = MyEntity.all(inMOC: moc, predicateFormat: : "foo == %d", someInt)
// myEntities is inferred to be of type [MyEntity]
In this use, Self
is the static type of the derived usage.