Sequence generator as extension function call fails with "receiver type mismatch"

Viewed 4403

I'm playing around with trying to generate a sequence out of a single Long value prepended to a LongRange. This works:

val seq = buildSequence<Long> {
    yield(2)
    yieldAll(3L..5)
}

But trying to generalize it, I can't seem to structure an extension function that I can call successfully:

infix fun Long.join(R: LongRange): Sequence<Long> {
    val start = this
    return buildSequence<Long> {
        yield(start)
        yieldAll(R)
    }
}

When I attempt to call it:

(2 join 3..5).forEach { /* do something */ }

I get

Error:(26, 20) Kotlin: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public infix fun Long.join(R: LongRange): Sequence defined in main.kotlin

It seems as though the compiler recognizes that the signature of the function is close to what I'm trying to achieve, but I'm clearly fuzzy on what "receiver type mismatch" means.

1 Answers
Related