What is the use of Sequence<T>.asSequence()?

There’s an extension function in the standard library for Sequence, its implementation is as follows:

/**
 * Returns this sequence as a [Sequence].
 */
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.asSequence(): Sequence<T> {
    return this
}

At first sight it doesn’t look like this function has any use at all, it returns the exact same object.

What was the reason for adding this function?

1 Like

It doesn’t hurt. Since it just returns the receiver and is inline only it will never effect the generated bytecode. The upside of this function is that it makes it easy to switch between iterables and sequences. AFAIK sequences and iterables have exactly the same functions so having this function makes sense from this standpoint.

3 Likes