Peter
10/22/2021, 4:38 PMurl
has mutable state constrained locally and should still be referentially transparent i believe, does this pattern have a place in functional kotlin?Emil Kantis
10/22/2021, 5:24 PMraulraja
10/22/2021, 5:41 PMpublic var
like this because mutability would escape the original definition allowing all var
properties accesible for mutation outside if you pass builder as return or argument value to other functions. I believe the same can be accomplished with constructors and default value arguments without leaking the mutable pieces.
For example:
public var encodedUser: String? = user?.encodeURLParameter()
public var user: String?
get() = encodedUser?.decodeURLPart()
set(value) {
encodedUser = value?.encodeURLParameter()
}
may be expressed as
public class URLBuilder(
private user: String?,
val encodedUser: String? = { user -> user?.encodeURLParameter() }
) {
val user: String? = encodedUser?.decodeURLPart()
}
IMO If the answer is just to give user =
syntax inside the block, it’s questionable if better ergonomics that can potentially leave the door open to incorrectness is worthy. Do you have a particular model in mind or is this in general for any builder?Peter
10/22/2021, 5:49 PM