Andrew O'Hara
11/11/2024, 9:07 PMclass BusinessLogic(
val getMessage: () -> String
)
I want to build a factory method that would let me override getMessage
with some static String.
fun createBusinessLogic(messageOverride: String? = null): BusinessLogic
I initially came up with this:
fun createBusinessLogic(
messageOverride: String? = null
) = BusinessLogic(
getMessage = if (messageOverride != null) {
{ messageOverride }
} else {
val random = Random(1337)
{ "Message ${random.nextInt()}" }
}
)
Which fails to compile, because the compiler thinks the () -> String
I'm trying to return is an argument to the Random
constructor. My workaround was to assign the lambda to a val
, and then return that on the next line.
fun createBusinessLogic(
messageOverride: String? = null
) = BusinessLogic(
getMessage = if (messageOverride != null) {
{ messageOverride }
} else {
val random = Random(1337)
val supplier = {
"Message ${random.nextInt()}"
}
supplier
}
)
But it's weird! And ugly! I feel like there should be a better way to do this, without resorting to something drastic like:
fun createBusinessLogic(
messageOverride: String? = null
) = if (messageOverride != null) {
BusinessLogic { messageOverride }
} else {
val random = Random(1337)
BusinessLogic { "Message ${random.nextInt()}"}
}
Which won't really fly in my real app, because the real-life BusinessLogic
has several more arguments that would be duplicated.ephemient
11/11/2024, 9:14 PMval random = Random(1337)
({ "Messagee ${random.nextInt()}" })
may be the easiest workaroundephemient
11/11/2024, 9:15 PMRandom(1337).let { random ->
{ "Message ${random.nextInt()}" }
}
val random = Random(1337)
fun() = "Message ${random.nextInt()}"
ephemient
11/11/2024, 9:17 PMval random = Random(1337);
{ "Message ${random.nextInt()}" }
but I think ;
breaks Kotlin style more than the other optionsAndrew O'Hara
11/11/2024, 9:18 PM({ "Message ${random.nextInt()}" })
I also completely forgot that ;
was an option to break up the clauses, and the editor doesn't even complain about it being redundant. But still, nice to avoid using ;
.