0

I am using Kotlin since few time and I found some code that I really dont understand why is for. Testing it seems like the return is Unit

basically I did this:

sealed class User {

}

data class ActiveUser(val name: String, val lastName: String, val email: String) : User () {

fun <T> doSomething(name: String.() -> T?) =
        name(this.email) ?:  throw RuntimeException("Error")

}

Create a class from where I inherit ActiveUser and receive some parameters.

I dont understand why is like this

String.() -> T?

I found the info for that https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver

But still I dont understand…

and the most shocking for me… how is possible that the parameter name can be used a method?

name(this.email)

what does it do? what does it actually mean?

Debugging it seems like do nothing... how is this possible?

I test it this way, maybe Im missing something

fun main() {
    val activeUser: ActiveUser
    activeUser= ActiveUser("John", "Doe", "john@doe.com")
    activeUser.doSomething {"some name" }
}

Do you have any ideas?

Ircover
  • 2,406
  • 2
  • 22
  • 42
jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • https://stackoverflow.com/questions/47329716/what-is-a-purpose-of-lambdas-with-receiver – Tim Jun 06 '19 at 09:34
  • https://stackoverflow.com/questions/45875491/what-is-a-receiver-in-kotlin – Tim Jun 06 '19 at 09:35

0 Answers0