mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Method calls are dispatched incorrectly if entity type is referenced as supertype #91
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @andriichernenko on GitHub (Oct 7, 2016).
Consider the following class hierarchy:
Let's try to import a
Thingnow. The following code will work:but the following will not:
entityTypeis nowStorableEntity.Type. SinceimportUniqueObjectmethod relies on generic parameter to determine which type to use for static method calls this means that all invocations will be dispatched toStorableEntityinstead ofThing. Which in this concrete example means that the app will crash becauseStorableEntity.uniqueIDKeyPathis abstract.Here's the project which reproduces this issue:
CoreStoreImportCrasher.zip
@JohnEstropia commented on GitHub (Oct 7, 2016):
Hi, thank you for the feedback.
I'm not sure if this is something to "fix". This is just how Swift generics works. If you really need dynamic behavior,
Intoclauses actually support that:Use the initializer that accepts
AnyClassargument.Fromclauses also support this.@andriichernenko commented on GitHub (Oct 7, 2016):
@JohnEstropia well, your example actually crashes as well. Have you tried running the test project?
yes, that's why generics cannot be relied on for correct method dispatch. I created pull request #108 which fixes this issue by calling type methods on
entityClassfrom theIntoobject instead of calling them on the generic type. I'm not sure, however, if similar fixes are needed elsewhere.@JohnEstropia commented on GitHub (Oct 7, 2016):
Thanks for the PR! I'll check your project when I get access to Xcode later. Your PR looks good though, I'll look for other places similar.
@JohnEstropia commented on GitHub (Oct 8, 2016):
Merged your PR. Thanks again!
Will update the Swift 2.3 branch with the same fixes as well