3

With this example:

open class Parent {
    fun some():Parent {
        return this;
    }
}

class A : Parent(){
    val name:String? = null;
}

But then this code results in an error:

val a = A().some().some()
a.name // ERROR

EDITOR NOTE: based on comments of the author to answers below, the question is NOT about referencing a.name but really is about something like "how do I get the instance of the class or its name that first started the chain of method calls". Read all comments below until the OP edits this for clarity.

my final goal is to return caller's type and can call this caller's instance property, no more as , no more override, any idea?

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
junk
  • 917
  • 3
  • 11
  • 29
  • I cleaned up the question and added the open keyword you mentioned in a comment was already there, so people do not miscontrue what the problem might be – Jayson Minard May 26 '17 at 17:32
  • Based on your comments you are not saying what your actual question is. You are making it look like you have a problem referencing a property of class `A` but that is not what you are trying to do at all, you should update the question based on your comments. I added an editors note which you should remove and replace with a clarifying message of your own. – Jayson Minard May 26 '17 at 17:33

5 Answers5

1

Just like java, you can use stackTrace's getMethodName(). Refer to the kotlin doc.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
LF00
  • 27,015
  • 29
  • 156
  • 295
0
open class Parent{
    open fun foo(): Parent {
        return this;
    }
}

This is your Parent class. Parent class has a method named foo(). foo() is a method of class A which will return the instance of it's own class. We must have to open the class and method because by default their visibility modifier is final.

class A : Parent() {
    override fun foo(): A { return this }
}

This is a class named A which extends Parent class. foo() is a method of class A which will return the instance of it's own class.

We will call it like this:

var a = A().foo().foo()
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
0

Actially your example is working(if you add open keyword because all classes in Kotlin are final by default:

A.kt

 open class A {
    fun some(): A {
        return this
    }
 }

B.kt

 class B : A() {
     val test = "test"
 }

And usage

val tmpB = (B().some().some() as B)
val test = tmpB.test

Edited: It because function some() return parent class which doesn't have child class property. So you need to cast it to child class.(Update code)

Dmitrii Nechepurenko
  • 1,414
  • 1
  • 11
  • 13
0

Your class always return Parent instance. This class do not have any field with the name name. To do that you have 2 ways:

The first:

open class Parent{
    fun some():Parent{
        return this
    }
}

class A :Parent(){
    val name:String? = null
}

fun main() {
    val a = (A().some().some() as A)
    a.name = "";
}

The second:

open class Parent{
    open fun some():Parent{
        return this
    }
}

class A :Parent(){
    override fun some():A {
        return this
    }

    val name:String? = null
}

fun main() {
    val a = A().some().some()
    a.name = "";
}
Trần Đức Tâm
  • 4,037
  • 3
  • 30
  • 58
-2

i have know how to do this:

@Avijit Karmakar

@Trần Đức Tâm

use inline function

 inline fun <reified T:Any> some(sql: String):T {

        return this as T ;
    }
junk
  • 917
  • 3
  • 11
  • 29