I would like to add an extension to Dictionary<Hashable, RangeReplaceableCollection>
which would add some functionality only for RangeReplaceableCollection where RangeReplaceableCollection.Element is Value type
I would like to append value a collection which is a value in the dictionary - basically from here. As it was pointed out in comments there is shorter way to do that. One can append to a dict with default value using dict[key, default: value]
although there is a note in Dictionary documentation:
Do not use this subscript to modify dictionary values if the dictionary’s Value type is a class. In that case, the default value and key are not written back to the dictionary after an operation.
So the naive way to implement my extension was something like this:
extension Dictionary
where Value: RangeReplaceableCollection
where RangeReplaceableCollection.Element is ValueType {
I was wondering if it is possible to achieve something like that (and if yes how?).
I have also thought about something like:
protocol ValueRangeReplaceableCollection : RangeReplaceableCollection where RangeReplaceableCollection.Element: Int {}
to use later as a restriction on value but I couldn't come up with a way to properly do it.
I would appreciate any thoughts on this.
EDIT: I understand that you can just use the extension from the gist and not care about value/reference types. My question is more about Swift generics.