-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add support for Iterable to sizeIs #11047
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
base: 2.13.x
Are you sure you want to change the base?
Conversation
@scala/collections @NthPortal |
the problem is,
this does not work when comparing two collections. if we take your example of if you can come up with an API that reads fine as prose in English, I would love to add it, but the best I've ever been able to come up with is † Personally, I sometimes read |
Hmm. Perhaps we could provide a different way to do this that reads better, but since universal equality means we have no way of preventing |
Mhm.. Maybe |
Tough one. I think that ideally the name should convey that you can use it to compare the size without actually calculating it, to distinguish it from Perhaps |
or I'm not sure it's actually worth changing, though, given that the |
I don't actually think we should change the behaviour of
I disagree with this. there is a strong API correspondence with the a.size.compare(b.size) <-> a.sizeCompare(b)
a.size.compare(4) <-> a.sizeCompare(4)
(a.size < 4) <-> (a.sizeIs < 4) calling it a.size.compare(4) <-> a.sizeCompare(4)
(a.size < 4) <-> (a.sizeComp < 4) I've been thinking about this a lot, and had a thought: there's a discrepancy in the a.size.compare(b.size) <-> a.sizeCompare(b) there are two ways to think of (a.size < b.size) <-> (a.sizeIs < b) does that read clearly/cleanly? can/does one's mind translate " thoughts? |
since it hasn't been linked already, prior discussion happened at scala/collection-strawman#338 and #6950 |
This allows the
sizeIs
function to also cover thesizeCompare(Iterable[_])
variation.The main motivation for adding this is that
sizeIs
currently returns an unintuitive result when used in the formcol1.sizeIs == col2.sizeIs
. This notation is not documented and likely not intended, but it is valid and it feels intuitive that it should result in comparing the size of the two collections.The reason for that is that
col1.sizeIs == 5
is the functional equivalent ofcol1.size == 5
and callscol1.sizeCompare(5) == 0
. Given thatsizeCompare
has a second implementation:sizeCompare(Iterable[_])
, I would expect thatcol1.sizeIs == col2.sizeIs
would be the functional equivalent tocol1.size == col2.size
and callcol1.sizeCompare(col2) == 0
. The problem is that the syntaxcol1.sizeIs == col2.sizeIs
is valid, but this is becausecol1.sizeIs
returns a value class for col1. This causes the==
operator to be handled byAny.==()
which calls theequals
method for the collections.So the result doesn't represent the equality of the sizes of the collections, but the equality of the collections itself. So currently
Seq(1,2).sizeIs == Seq(2,3).sizeIs
results infalse
.