Sam YC
Sam YC

Reputation: 11637

Kotlin function declaration with dot, what does it mean?

abstract class Children : Parent {
    
    fun create(
        param1 : String,
        param2 : String,
        param3 : String

    ): (MyWork) -> Unit = 
        createWork(param1, param2 + param3)

    fun Parent.createWork(
        param1 : String,
        param2 : String
    ): (MyWork) -> Unit = 
        createWork(
            param1 + param2
        )


    private fun Parent.createWork(
        param1 : String
    ): (MyWork) -> Unit = { work: MyWork ->
        work.name = param1
    }
}

I am recently learning Kotlin for one of my project, I encouter these piece of code, this is first time I see Kotlin function with a dot there Parent.createWork, I checked the Parent interface, there is no such function createWork, what does it mean here actually? I have searched this, but only found something so-called "dot operator", but it is not the same thing. Can anyone point me to the right place?

Upvotes: 2

Views: 2341

Answers (1)

Henry Twist
Henry Twist

Reputation: 5990

This is called a Kotlin extension function which allows you to add new class functionality without inheritance. You can read more about them in the documentation.

Upvotes: 2

Related Questions