Fluent methods for data class in kotlin

后端 未结 3 1007
旧巷少年郎
旧巷少年郎 2021-01-13 05:09

We are familiar with fluent interfaces for calling methods in java and other programming languages. For eg:

Picasso.with(this).load(url).into(imageView);
         


        
3条回答
  •  半阙折子戏
    2021-01-13 06:06

    For accessing properties in Kotlin, we don't use getter and setter functions, and while you can override their setters to give them custom behaviour, you can't make them return anything, because calling them is an assigment, which is not an expression in Kotlin. As an example, this doesn't work:

    var x = 1
    var y = (x = 5) // "Assigments are not expressions, and only expressions are allowed in this context"
    

    What you can do is define additional methods that set them, and then return the instance they were called on. For example:

    data class Name(var firstName: String = "", var lastName: String  =  "") {
        fun withLastName(lastName: String): Name {
            this.lastName = lastName
            return this
        }
    }
    
    val name = Name(firstName = "Jane") 
    name.withLastName("Doe").withLastName("Carter")
    

    Note that if you name this method setLastName, you'll have problems with using the class from Java, because the lastName property is already visible through a method called setLastName when you're writing Java code.

    The perhaps more usual solution in Kotlin though is to replace fluent builders by using apply from the standard library, as it was already described in Christian's answer.

提交回复
热议问题