Gustav Elmgren
08/09/2022, 1:59 PMFlights
.join(...)
.join(...)
And then I have an optional join
based on another parameter. One solution I guess is to use a var:
var joins = Flights
.join(...)
.join(...)
if (condition) { joins = joins.join(AnotherTable...) }
Is there any feature in kotlin that make it possible to do the if in the assignment so I can keep it as a val
? (or something like adjustWhere
in Exposed)
Flights
.join(...)
.join(...)
.someMagicKotlinMethod {
if (condition) ...
}
phldavies
08/09/2022, 6:08 PM.let { if (condition) it.join(other) else it }
(https://kotlinlang.org/docs/scope-functions.html#let)Kamil Kalisz
08/11/2022, 1:40 PMfun Join.conditional(condition: Boolean, conditionalBlock: (Join) -> Join): Join{
return if(condition) {
conditionalBlock.invoke(this)
} else this
}
and use it in this way:
NoteTable.join(EarthTable, joinType = JoinType.INNER)
.conditional(true){
it.crossJoin(WaterTable)
}.conditional(false){
it.fullJoin(SunTable)
}.join(IceTable, joinType = JoinType.CROSS)
Kamil Kalisz
08/11/2022, 1:43 PMfun Join.conditional(condition: Boolean, conditionalBlock: Join.() -> Join): Join{
return if(condition) {
conditionalBlock.invoke(this)
} else this
}
NoteTable.join(NoteTable, joinType = JoinType.INNER)
.conditional(true){
crossJoin(NoteTable)
}.conditional(false){
fullJoin(NoteTable)
}.join(NoteTable, joinType = JoinType.CROSS)
Gustav Elmgren
08/11/2022, 3:25 PMphldavies
08/11/2022, 4:47 PMinline fun <T> T.letIf(condition: Boolean, block: (T) -> T) = if(condition) let(block) else this
or
inline fun <T> T.runIf(condition: Boolean, block: T.() -> T) = if(condition) block() else this
(named consistently with `let`/`run` )
example usage:
Flights
.join(...)
.runIf(condition) { join(...) }
.letIf(condition) { it.join(...) }
Kamil Kalisz
08/11/2022, 4:50 PMphldavies
08/11/2022, 5:12 PMletIf
) - an more (contrived) example: https://pl.kotl.in/iLYHl-LOH