Is there any reason why `Sequence<T>.drop(n:...
# stdlib
j
Is there any reason why
Sequence<T>.drop(n: Int)
param type is
Int
instead of
Long
? I thought Kotlin’s sequence is equivalent to Java’s stream, and presuming the type should be the same as
Stream<T> skip(long n);
r
I thought Kotlin’s sequence is equivalent to Java’s stream
No, they're not. They are similar in a number of ways, but are definitely not equivalent.
j
In what aspect? From my understanding, they are all representing a possible infinite stream of data, and can do minimal & lazy calculations
r
There's a bit of a logical jump between "these patterns have some similar high level concepts" and "these patterns are exactly equivalent". You're explanation applies just as well to flows, reactive streams, and all sorts of other vaguely similar but not equivalent patterns.
🤔 1
j
Thanks for your explanation, but back to the question, I still get confused why it's an Int but not a Long, since it can represent an infinite flow
r
Sure, but then why not a BigInteger? Long isn't infinite either. And since Kotlin doesn't do automatic widening, it would be annoying to have
toLong()
everywhere. Better to stick with int as that's what's used for sizes and indexes everywhere else in the stdlib.
To be clear, I'm not saying that is the answer (or that there even is a good answer), just my thoughts on the matter.
j
yeah, this makes sense. I just wonders if there’s some special thinkings behind when implementing this function
e
Sequence effectively wraps an Iterator, Stream effectively wraps a Spliterator, these are not the same
in particular, collections are expected to be representable in-memory and thus are explicitly limited to int sizes, which extends through iterators and sequences, while spliterators are generalized to larger and parallel streams
👍 4