<div dir="ltr">Currently you can constrain a type parameter via a generic using equality<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>protocol A {</div></div><div><div> associatedtype T where T == BKind.V</div></div><div><div> associatedtype BKind: B</div></div><div><div>}</div></div><div><div><br></div></div><div><div>protocol B {</div></div><div><div> associatedtype V</div></div><div><div>}</div></div></blockquote><div><div><div><br></div><div>However, you cannot do the following</div><div> </div><div> associatedtype T where T: BKind.V</div><div><br></div><div>Could this be added to the language? Is there a reason something like this would not be sound?</div><div><br></div><div>My use case would look like the following</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><div>protocol Mapping {</div></div></div><div><div> associatedtype MappedObject: DatabaseAdapterKind.BaseType</div></div><div><div> associatedtype DatabaseAdapterKind: DatabaseAdapter</div></div><div><br></div><div> ...</div><div><div>}</div></div><div><div><br></div></div><div><div>protocol DatabaseAdapter {</div></div><div><div> associatedtype BaseType</div></div><div> </div><div> ...</div><div><div>}</div></div></blockquote><div><div><br></div><div>For Realm Obj-C fulfillment would look like the following</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>// User is an RLMObject subtype</div></div><div><br></div><div><div>protocol UserMapping: Mapping {</div></div><div><div> typealias MappedObject = User</div></div><div><div> typealias DatabaseAdapterKind = RealmAdapter</div></div><div><div><br></div></div><div><div> // ... functions</div></div><div><div>}</div></div><div><div><br></div></div><div><div>protocol RealmAdapter: DatabaseAdapter {</div></div><div><div> typealias BaseType = RLMObject</div></div><div><div><br></div></div><div><div> // ... functions</div></div><div><div>}</div></div></blockquote><div><div><br></div><div>For CoreData replace RLMObject with NSManagedObject</div><div><br></div><div>The mapping system allows the developer to write a mapping from JSON to a type (MappedObject). Given that the type of MappedObject is a subtype of the BaseType of the DatabaseAdapter the system will write an object of type MappedObject to the database. This setup works with Realm Obj-C, RealmSwift, and Core Data all of which have different BaseTypes but use subtyping from a BaseType to specify the Model/MappedObject that is stored in the db.</div><div><br></div><div>I can't use MappedObject == DatabaseAdapterKind.BaseType because this needs to be a subtyping relation. E.g. a User: NSManagedObject is true but User == NSManagedObject is not.</div><div><br></div><div>Since I can't specify this subtype relation in the protocol I end up having to do runtime type checking and as! casts in quite a few places which seems unnecessary and error prone during refactoring.</div><div><br></div><div>-- <br><div class="gmail_signature"><div dir="ltr"><span><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="font-size:16px;font-family:Arial;background-color:transparent;font-style:italic;vertical-align:baseline;white-space:pre-wrap">Rex Fenley</span><span style="font-size:16px;font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap"> </span><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">|</span><span style="line-height:1.15;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap"> </span><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">IOS DEVELOPER</span><br></p></span><span><br><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><img src="https://lh5.googleusercontent.com/xMgzw3JkFL3DLkdwyq0WxJzKs_XP57gVVCaBMvgi1FKCjSeue0xdx3JZeCWBlxN4KRHhHOfdvJbc1N-AjTwXcKIq4cjJg9H7iaFpQ8WbO4N3c9Y5dzi19cPOs_owPquuqw" width="250px;" height="53px;" style="border: none;"></span></p><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><a href="https://www.remind.com/" style="text-decoration:none" target="_blank"><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);font-weight:bold;text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent">Remind.com</span></a><span style="font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"> </span><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent">| </span><a href="http://blog.remind.com/" style="text-decoration:none" target="_blank"><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent">BLOG</span></a><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"> | </span><a href="https://twitter.com/remindhq" style="text-decoration:none" target="_blank"><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent">FOLLOW US</span></a><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"> | </span><span style="font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"> </span><span style="text-decoration:underline;font-size:11px;font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><a href="https://www.facebook.com/remindhq" style="text-decoration:none" target="_blank">LIKE US</a></span></p></span></div></div>
</div></div></div>