Scope of extension functions


#1

Am a bit confused about this. I have an extension function like this:

public val <T : Comparable<T>> Entry<T>.firstKey : T get() = this.getKey(0)

This (and others) are defined in a dedicated EntryFuncs.kt file.

This works fine for most of my code but in a test class the compiler says ‘Unresolved reference: firstKey’.

However, if I copy and paste the above line as-is into my test Kotlin file at the top, it works :S

I thought it might be something to do with main vs. test classes, but if I move my test class into src/main/java, problem remains.

It’s like the compiler isn’t “seeing” the extension function from some classes.

Have tried to re-create a simpler example of my project layout to pin this down but can’t seem to.

Any help appreciated!
Alfie.


#2

Did you import this function in the test class?


#3

Hi Andrey, thanks for quick reply! Yes indeed that was the problem - how simple - duh!

It would be nice if the Kotlin plugin prompted to import in this case. I think it may also be good if, when moving a Kotlin file to another package, it prompted to change the package declaration in the file. I moved my test class to the same package to see if it would make a difference and of course it didn’t, because it stayed in the original package.

Thanks. Am really enjoying Kotlin!
Alfie.


#4

I found elsewhere the plugin is prompting me to import the function - so probably just user error.