-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection
conformance for LeftValues
and RightValues
#7
Conversation
Your code looks good. I just see one issue; accessing var dict: BijectiveDictionary = [
"a": 1,
"b": 2,
"c": 3
]
print(dict.leftValues[dict.startIndex])
print(dict.rightValues[dict.startIndex])
// error: Subscript 'subscript(_:)' requires that
// 'BijectiveDictionary<String, Int>.Index' conform to 'RangeExpression' However, this is possible with var dict = [
"a": 1,
"b": 2,
"c": 3
]
print(dict.keys[dict.startIndex])
print(dict.values[dict.startIndex]) I need to do some investigation to determine the source of this issue. Please let me know if you have any ideas. I would recommend taking a look at |
Thanks for catching that. We should add it to the tests. Is it worth looking into if we should make our Index conform to RangeExpression? |
@DandyLyons, please review PR #8. I realized there is a bug in the existing implementation that prevents |
The |
Sounds good. I'll push a revision. |
Could I back it by a I was thinking of using a similar pattern to what you use here: BijectiveDictionary/Sources/BijectiveDictionary/BijectiveDictionary+Collection.swift Line 12 in 233a2f6
|
extension BijectiveDictionary.LeftValues: Collection {
public typealias Index = BijectiveDictionary<Left, Right>.Index
@inlinable public var startIndex: Index { Index(_ltr.startIndex) }
@inlinable public var endIndex: Index { Index(_ltr.endIndex) }
@inlinable
public func index(after i: Index) -> Index {
Index(_ltr.index(after: i._ltrIndex))
}
@inlinable
public subscript(position: Index) -> Left {
_ltr[position._ltrIndex].key
}
} |
There are multiple routes that I'm exploring to search for a solution. But I have a few questions: |
Should BijectiveDictionary/Sources/BijectiveDictionary/BijectiveDictionary+Collection.swift Line 12 in 9b84532
Or should it be an internal type with a public interface through a typealias. If we use the second approach, then this could help me to disambiguate, but it is a pretty large refactor that I don't want to start without your design approval. |
But then yet another approach would be that we should not reuse So another approach would be to design a concrete |
In this case, I think it makes sense for Please let me know if this answers your question. |
That makes a lot of sense and I didn't consider that it would preserve the type relationship. I'll look more at the |
I apologize. This was completely accidental. I synced my fork so I could get the PR #8 that you merged and somehow this closed my pull request and force pushed and closed the PR. |
The Collection conformance now has the change that you requested. P.S. Sorry this took so long. |
No problem, there's no deadlines here. Looks good to merge. |
This PR adds
Collection
conformance toLeftValues
andRightValues
.Design Considerations
The intention is to map the values:
LeftValues._ltr.keys
RightValues._rtl.keys
Hopefully this should allow the user to just treat a
BijectiveDictionary
as if it were a vanillaDictionary
, except they would use.leftValues
and.rightValues
instead of.keys
and.values
.Order Preservation
I noticed that the docs now mention that
LeftValues
does not guarantee the order and that it might guarantee it in a future release. I think it makes sense not to guarantee the order, since the vanillaDictionary.Keys
does not guarantee the order.