I have the following:
package mypkg.util
object SequenceExt {
fun <T : Any> Sequence<T?>.takeUntilNull(): Sequence<T> {
// Unchecked cast, oh well, it's erased
return this.takeWhile({ it != null }) as Sequence<T>
}
}
First question: So I noticed I cannot import *
from this in a separate package (e.g. import mypkg.util.SequenceExt.*
) because I get “Cannot import-on-demand from object ‘SequenceExt’” in the IDE. Why is this? If I choose to organize my extensions inside an object instead of at the package level (I don’t want to clutter up “util” and don’t want a new package), do I have to manually import each method (e.g. “import mypkg.util.SequenceExt.takeUntilNull”)? This makes it tough for IDE discovery.
Second part: I am having trouble using this extension from the same package. In addition to the code above, I have the following in an adjacent file:
package mypkg.util
import SequenceExt.takeUntilNull
object CollectionExt {
fun <T : Any> Collection<T?>.takeUntilNull(): Collection<T> {
return this.asSequence().takeUntilNull().toList()
}
}
But I get an IDE error on the import. Is this is a bug or is there something I am misunderstanding about singleton static imports in the same package that prevents it from happening?
Also, kinda third thing: I am really just needing a Scala-like collect (i.e. map + filter in the same iteration) and lazy take. Is my approach ideal (even though I am casting to non-null type param to avoid an iteration knowing I can save it with type erasure)?
Thanks for the help (sorry for several forum posts, I am trying to build a project and hitting a lot of road blocks).