Kotlin inner class can't resolve extension function declared in outer class - kotlin

Why inner class in Kotlin can't not access extension function declared in outer class as below:
class A{
val a = "as".foo() // LINE 1
class B{
val b = "as".foo() // LINE 2
}
fun String.foo(){}
}
On LINE 1 extension function is resolved but on LINE 2 the function is not resolved. Wonder why there is such limitation?

This is not an inner class, because you didn't use the keyword inner on it. It is merely a nested class. If you're familiar with Java, it's like a static inner class. Since it is not inner, it does not have any implicit reference to the outer class, and cannot make bare calls to members of the outer class since there is no specific instance to use the members of. It can however call members of the outer class on an instance of the outer class, so you could for example do the following:
class A{
val a = "as".foo()
class B{
val b = A().run { "as".foo() }
}
fun String.foo(){}
}
Even though foo is an extension function, it's also a member of A because of where it's declared. Using a scope function that causes a class to be a receiver inside the scope is one way to call one of its member extension functions from another class.
EDIT: Here's an example of one reason you'd want to declare an extension inside a class.
class Sample(val id: Int) {
private val tag = "Sample#$id"
fun String.alsoLogged(): String{
Log.d(tag, this)
return this
}
}
You can use this extension to easily log Strings you're working with inside the class (or when it's the receiver of run or apply). It wouldn't make sense to declare outside the class because it uses the private tag property of that class.

It's because Kotlin compiles your code to
public final class A {
#NotNull
private final Unit a;
#NotNull
public final Unit getA() {
return this.a;
}
public final void foo(#NotNull String $this$foo) {
Intrinsics.checkNotNullParameter($this$foo, "$this$foo");
}
public A() {
this.foo("as");
this.a = Unit.INSTANCE;
}
public static final class B {
public B() {
// You can't access A's foo() method here.
}
}
}

Related

Parameterized Test in Kotlin usign mehtod source for nested test class

I may use case the class under test has many cases so it is divided into a structure of inner classes. I want to write parameterized test cases to reduce boiler plate and code duplication.
For this I wanted to go with the approach of method source.
Class under test
class UnderTest
{
testThisMethod(a:String,b:String?){
// Do something
externalInterface.call(a?:b)
}
}
Test Case structure
internal class A() {
private val externalService = mockk<ExternalService>
private val test: UnderTest(externalService)
// Some general tests
//---Position Outter
inner class A {
//--- Position A
inner class B {
//--- Position C
#ParameterizedTest
#MethodSource("provideArguments")
fun `with arguments external service create object`(
argument1: String,
argument2: String,
expected: String
) {
// Some code
verify {
externalService.call(expected)
//some more verification
}
}
}
}
}
To provide the argument provider method I tried placing it at positions and got following errors
Position outer: initialization error :Could not find factory method in class
Position A,B: compilation error: companion not allowed here
How can this be achieved?
Try using #TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class A() {
private val externalService = mockk
private val test: UnderTest(externalService)
// Some general tests
//---Position Outter
inner class A {
//--- Position A
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class B {
//--- Position C
#ParameterizedTest
#MethodSource("provideArguments")
fun `with arguments external service create object`(
argument1: String,
argument2: String,
expected: String
) {
// Some code
verify {
externalService.call(expected)
//some more verification
}
}
}
}
}
Check here for more details

How to access class constructor parameters in a companion object

I would like to access the arguments passed to the class constructor in a companion object :
class Home(private val activity: String) {
companion object {
fun doSomething() {
println(activity)
}
}
}
fun main() {
Home("Hello World").doSomething()
However, an error is raised when I run this code saying that activity is unresolved reference
A companion object is basically the equivalent of a Java static nested class (and that's how it's implemented under the hood), which means an instance of the static nested class could exist even with no instance of the outer class.
Example:
class Outer(private val activity: String) {
companion object {
fun doSomething() {
println("Hello, world!")
}
}
}
fun main() {
Outer.doSomething() // no new Outer instance here
}
If you want a non-static nested class (aka inner class) you can then reference properties of the outer class from within the inner class. That's because instances of the inner class are tied to instances of the outer class.
Example:
class Outer(private val activity: String) {
inner class Inner {
fun doSomething() {
println(activity)
}
}
}
fun main() {
Outer("Hello, world!").Inner().doSomething()
}
For more info, you can also have a look at Java documentation about nested (aka static) and inner (aka non-static) classes here: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Access a Kotlin Outer class using the inner object from somewhere else

I have the following setup:
class A {
fun runA() {
}
inner class B {
fun runB() { // Proxy method for runA()
runA() // its okay to call it here
}
}
}
fun test() {
val obj = A().B()
obj.runA() // how to call this one??
obj.runB() // i do not want to add a proxy method for everything in A
}
Is there any way to call runA() when I only have the B object?
Every instance of B is always coupled to an instance of A, so it should theoretically be possible (and as I am able to call runA() from within B proves that)
Best solution that I currently have is providing a accessor of A within B like so:
inner class B {
fun a(): A = this#A
fun runB() {
runA()
}
}
and then call it like obj.a().runA()
Would be nice if I could just directly call obj.runA(), I don't see a reason why it shouldn't be technically possible other than the compiler not allowing it.
Thanks for your input!
B is not A. The compiler gives B a private reference to his own A, because he needs it to execute runA. But it is private, doesn't mean that you can access from there. You can just write runA and it works inside runB because an inner class has all the members of the parent class in scope, kind of like when you use a closure. See the analogy with this example:
class A {
fun runA() {
}
fun B {
{
runA() // its okay to call it here
}
}
}
fun test() {
val obj = A().B()
obj()
}
Similarly to what you saw with the inner class, inside the closure object created when you call the member B() you can access to all variables in scope (including the instance of A that was used to invoke B()).
If you want an object B that has all the members of A you should probably have a look at inheritance instead of inner classes. Or expose an accessor to A instance like you did.

How can I use a variable in my inner class?

I have this code using Kotlin:
class A{
var myVariable:String?=null
constructor(myVariable:String){
this.myVariable = myVariable
}
init {
println("Success !")
}
inner class B{
init {
println(myVariable)
}
}
}
fun main(args:Array<String>){
var b = A("test").B // this does not work
}
And unfortunately it does not work I get:
Error:(20, 23) Kotlin: Nested class 'B' accessed via instance reference
Error:(20, 23) Kotlin: Classifier 'B' does not have a companion object, and thus must be initialized here
How can I solve my problem?
When you do:
A("test").B
You're saying "Fetch something from an instance of A" - in this case, it's a class. However, this isn't the syntax you're looking for in your case. You can get mostly anything, but getting a reference is a separate issue. If you want to get a function, the syntax is different from getting a field or calling a function. Although this isn't very important, bit might still be worth keeping that in mind.
Since B is an inner class, you're so far entirely correct that you need an instance of A first. But you also need an instance of B. When you initialize B, it's still "connected" to the parent class, which is why you can access outer variables without any problems. However, it's still an initializable class - and you need to initialize it.
So you need to initialize B as well. The Kotlin syntax for this is pretty nice (where as the syntax in Java is slightly horrible) - all you need to add is () at the end.
So you'll end up with this:
val b = A("test").B()
// ...
This is because it's an inner class. If you had a static inner class (in Kotlin, that's a class within a class without the inner keyword), the initialization would've been A.B() - A isn't initialized in those cases.
You can also split up the initialization:
val a = A("test")
val b = a.B();
Once you have the variable, it's exactly like every other variable - the only difference here is the initialization.
Try this:
class A{
var myVariable:String?=null
constructor(myVariable:String){
this.myVariable = myVariable
}
init {
println("Success !")
}
inner class B{
init {
println(myVariable)
}
}
}
fun main(args:Array<String>){
var b = A("test").B() // You have to call the constructor here
}
You should create an instance (call constructor) of class B before accessing its members:
fun main(args:Array<String>) {
val b = A("test").B()
b.someFunction()
}
class A {
var myVariable:String? = null
constructor(myVariable: String) {
this.myVariable = myVariable
}
init {
println("Success !")
}
inner class B {
init {
println(myVariable)
}
fun someFunction() {
myVariable = "set new value to the variable"
}
}
}

OOP question involving the best way to reference a base class protected variable without having to typecast every-time it is used

I have a quick OOP question and would like to see how others would approach this particular situation. Here it goes:
Class A (base class) -> Class B (extends Class A)
Class C (base class) -> Class D (extends Class C)
Simple so far right? Now, Class A can receive an instance of Class C through its constructor. Likewise, Class B can receive an instance of either class C or Class D through its constructor. Here is a quick snippet of code:
Class A
{
protected var _data:C;
public function A( data:C )
{
_data = data;
}
}
Class B extends A
{
public function B( data:D )
{
super( data );
}
}
Class C
{
public var someVar:String; // Using public for example so I don't need to write an mutator or accessor
public function C() { } // empty constructor for example
}
Class D extends C
{
public var someVar2:String; // Using public for example so I don't need to write an mutator or accessor
public function D() { super(); } // empty constructor for example
}
So, let's say that I am using class B. Since _data was defined as a protected var in Class A as type C, I will need to typecast my _data variable to type D in class B every time I want to use it. I would really like to avoid this if possible. I'm sure there is a pattern for this, but don't know what it is. For now, i'm solving the problem by doing the following:
Class B extends A
{
private var _data2:D;
public function B( data:D )
{
super( data );
_data2 = data;
}
}
Now, in class B, I can use _data2 instead of typecasting _data to type D every-time I want to use it. I think there might be a cleaner solution that others have used. Thoughts?
I think B doesn't take C or D... in order for it to do what you wrote it should be
public function B( data:C )
{
super( data );
}
At least as far as I used to know :)
I doubt you can use a downwards inheritance in your case.
As for the pattern, the best one to use in situations like these is Polymorphism. Alternatively, depending on language, you can use interfaces. Or if languages allow it, even a combination of conventional code and templates.
Most modern OO languages support covariant of return type, that is: an overriding method can have a return type that is a subclass of the return type in the original (overridden) method.
Thus, the trick is to define a getter method in A that will return C, and then have B override it, such that it returns D. For this to work the variable _data is immutable: it is initialized at construction time, and from that point it does not change its value.
Class A {
private var _data:C;
public function A(data:C) {
_data = data;
}
public function getData() : C {
return _data;
}
// No function that takes a C value and assigns it to _data!
}
Class B extends A {
public function B(data:D) {
super(data);
}
public function getData() : D { // Override and change return type
return (D) super.getData(); // Downcast only once.
}
}
This how I usually write it in Java:
public class A {
private final C data;
public A(C data) { this.data = data; }
public C getData() { return data; }
}
public class B extends A {
public B(D data) { super(data); }
#Override
public D getData() { return (D) super.getData(); }
}