Kotlin with(receiver) equivalent in Dart

255 views Asked by At

Let's say I have a class like this :

class User {
    lateinit var firstname: String
    lateinit var lastname: String
    lateinit var age: String
}

Now, I want to populate this class, and use it, like this :

fun main() {
    val user = User()
    user.firstname = "firstname"
    user.lastname = "lastname"
    user.age = "25";
    println("Hello, ${user.firstname} ${user.lastname}, you are ${user.age} !")
}

Output : > Hello, firstname lastname, you are 25 !

In Kotlin, we have some syntactic sugar keyword with, which returns the receiver passed in params and allows us not to repeat the variable name over and over, for example, this will output the same thing and is a bit more pretty :

fun main() {
    val user = User()
    with (user) {
        firstname = "firstname"
        lastname = "lastname"
        age = "25";
        println("Hello, $firstname $lastname, you are $age !")
    }
}

Is there an equivalent to Kotlin's with(receiver: T, block: T.() -> R) function in Dart ?

0

There are 0 answers