Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected members not accessible in extension functions?

Kotlin features a couple of visibility modifiers as well as extension functions. The documentation states that Extensions are resolved statically. But what does this mean for the visibility of class members within extension functions?

Let's consider the following contrived example:

class A { protected val a = "Foo" }
fun A.ext() { print(a) } //Raises: Cannot access 'a': it is 'protected' in 'A'

class B { val b = "Bar" }
fun B.ext() { print(b) } //Compiles successful

The code will not compile. It seems that protected members are not accessible when extending the class.

So does resolved statically mean the extension function is syntactic sugar for having something like this in Java:

public static void ext(A receiver){ System.out.print(receiver.a); }

This would explain why protected members aren't accessible. On the other hand it's possible to use (and even omit) this in extension functions.

So what is the exact scope of extension functions?

like image 345
André Diermann Avatar asked Nov 22 '15 07:11

André Diermann


People also ask

Can extension functions access private members?

Extension methods cannot access private variables in the type they are extending.

Can extension methods access private members Kotlin?

If the extension is defined at the top level of the class, it can access all the private variables and functions of that class. If the extension function is defined outside the class, it can not access the private variables or functions of that class.

What is true about extension functions in Kotlin?

Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any type of design pattern. The created extension functions are used as a regular function inside that class. The extension function is declared with a prefix receiver type with method name.

What is the receiver in the extension function?

Declaring extensions as members An instance of a class in which the extension is declared is called a dispatch receiver, and an instance of the receiver type of the extension method is called an extension receiver.


1 Answers

You are correct, extension functions/properties are compiled to static JVM methods. Generally they are located in another class in some other package than the class they're extending, so it's not possible to call protected methods of that class due to VM accessibility rules. It's also consistent with the protected visibility definition (visible in the class and its subclasses): an extension function is not a subclass, nor is it defined in a subclass of the class you're extending.

The fact that you can use or omit this in the body of an extension function is only a syntactic feature, the compiler emits the required instructions to load the first parameter of the JVM method instead.

like image 61
Alexander Udalov Avatar answered Sep 20 '22 01:09

Alexander Udalov