I have java code that uses the pattern of say Inqu...
# getting-started
s
I have java code that uses the pattern of say Inquiry().status(sts).notes(blah). Which enables chaining. what can i do from a kotlin aspect to define a similar class as that? the fields are not required, so you may want to set only 2 of those 3
r
Assuming your java class is something like:
Copy code
public class Inquiry {
    private Status status;
    private Notes notes;

    public Inquiry status(Status status) {
        this.status = status;
        return this;
    }

    public Inquiry notes(Notes notes) {
        this.notes = notes;
        return this;
    }
}
You can have basically the same in Kotlin
Copy code
class Inquiry {
    var status: Stats? = null
    var notes: Notes? = null

    fun status(status: Status?): Inquiry {
        this.status = status
        return this
    }

    fun notes(notes: Notes?): Inquiry {
        this.notes = notes
        return this
    }
}
s
@Ruckus ah, what if you are using the ctor members? e.g. class Inquiry( val status: String? = null, ...)
just add the function in the brackets of the class, right?
r
Yeah, and make those `var`s instead of `val`s.
s
ah. that does get rid of the constness sadly ­čśĽ
r
They wouldn't be "consty" in the java version either.
s
right. guess i was trying to get the benefits of the constness but mabye it's just causing too many issues here
r
So you want the benefits of "constness", but you also want to set the values after construction. You either need to accept those two goals are mutually exclusive, or use an intermediary builder class of some sort.
s
indeed, i understand
r
As a side note, you generally don't need to bother with such pipeline functions in Kotlin. The stdlib extensions (like
apply
allow turning pretty much any thing into a pipeline:
Copy code
class Inquiry {
    var status: Status? = null
    var notes: Notes? = null
}

Inquiry().apply {
    status = sts
    notes = blah
}.doSomethingWithInquiry()
s
right but those are unusuable if you go the const route
as i understand it?
r
Indeed, that's why I said it's a side note. It won't help with that goal. I only wanted to make a subtle point that "how do I write Java code in Kotlin" is generally a bad place to start, as the two languages have different idioms.
If you're goal is "constness", and all you want is to set some (nut not necessarily all) parameters, default values are the way to go.
Copy code
Inquiry(status = sts, notes = blah)
is arguably more readable than
Copy code
Inquiry().status(sts).notes(blah)
s
yeah, that's why i was trying to bridge the gap between writing it differently and weighing the costs/downsides of writing it the 'correct' way
v
Or you make it a data class and use the
clone
method in the chain methods
e
the common patterns in Kotlin are either named arguments, or a builder/scope such as
Copy code
Inquiry {
    status = sts
    notes = blah
}.doSomethingWithStatus()
when there are concerns about binary compatibility