[WIP]: Reliant/Swift: Context substitution #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Exploring a way to register context substitutions so they can be swapped out for testing or other purposes.
Current implementation
I added a map to the context cache which will be accessible through
relyOnSubstitute(T)(T)
. This global function simply creates an entry in the map, whichrelyOn()
will then use to determine if it needs to create a substitute context or a normal one, when requested.Remarks
I have only gotten this to work half-way. The substitute type is registered and found correctly, but when its
createContext()
function is invoked, the original, non-substituted version is called becausecreateContext()
isstatic
with no way to override it, becausestatic
also meansfinal
in Swift.Because of how I set up the
relyOnSubstitute
's arguments (both of the same typeT:ReliantContext
), the second argument needs to resolve to the same type as the first. That is, the second argument needs to be covariant with the first. This is currently achieved by subclassing the original context, with currently no way to modifycreateContext()
's implementation due to the reason stated above.Subclassing doesn't feel right, so I'm currently exploring ways to change
relyOnSubstitute
s signature to something like:This causes serious type resolution problems of course, so I'm still trying to figure something out.