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?