Marc Knaup
12/11/2018, 8:18 AMinline fun <R> Boolean.thenTake(action: () -> R) =
if (this) action() else null
val result = if (input) something() else null
->
val result = input.thenTake { something() }
gildor
12/11/2018, 8:19 AMthen
returns null
gildor
12/11/2018, 8:20 AMval result = input.then { something() } ?: someDefault
Which is ridiculous replacement for if/elseMarc Knaup
12/11/2018, 8:22 AMmapTrueElseNull
?Marc Knaup
12/11/2018, 8:23 AMoverride fun visitExtensions(type: KmExtensionType) =
(type == JvmConstructorExtensionVisitor.TYPE).then {
object : JvmConstructorExtensionVisitor() {
override fun visit(desc: JvmMethodSignature?) {
jvmSignature = desc
}
}
}
override fun visitExtensions(type: KmExtensionType) =
if (type == JvmConstructorExtensionVisitor.TYPE) {
object : JvmConstructorExtensionVisitor() {
override fun visit(desc: JvmMethodSignature?) {
jvmSignature = desc
}
}
}
else
null
gildor
12/11/2018, 8:27 AM{}
second version is more readable for megildor
12/11/2018, 8:29 AMoverride fun visitExtensions(type: KmExtensionType): JvmConstructorExtensionVisitor {
if (type != JvmConstructorExtensionVisitor.TYPE) ?: return null
return object : JvmConstructorExtensionVisitor() {
override fun visit(desc: JvmMethodSignature?) {
jvmSignature = desc
}
}
}
elect
12/11/2018, 8:32 AMsomething().takeIf{ input }
Marc Knaup
12/11/2018, 8:33 AMMarc Knaup
12/11/2018, 8:33 AMsomething()
elect
12/11/2018, 8:33 AMMarc Knaup
12/11/2018, 8:35 AMgildor
12/11/2018, 8:36 AMelse
is the bestMarc Knaup
12/11/2018, 8:38 AMboolValue.thenTake { … }
which is basically the reverse of
{ … }.takeIf(boolValue)
except it's not evaluated unless boolValue
is true
miha-x64
12/11/2018, 8:57 AMinline fun <R : Any> Boolean.thenTake
Otherwise ?:
may lead to puzzlers.Marc Knaup
12/11/2018, 8:59 AM.takeIf()
?
Also allows null
.miha-x64
12/11/2018, 9:27 AMtakeIf
shouldn't allow nullable receiver. This makes ?:
ambiguous.Marc Knaup
12/11/2018, 11:05 AMlet
for example.Marc Knaup
12/11/2018, 11:06 AMflatMap
the double-optional into one.Marc Knaup
12/11/2018, 11:06 AMmiha-x64
12/11/2018, 11:44 AMfunctions likelet
let
has nothing to do with nullability.Marc Knaup
12/11/2018, 11:47 AMmayBeNull.let { mayReturnNull() }
<- no distinguishing between which one is null
mayBeNull.takeIf { false }
<- same
Where is the difference when it comes to the nullable receiver problem you mention?miha-x64
12/11/2018, 12:56 PMlet
is not about nullability. takeIf
is.Marc Knaup
12/11/2018, 12:57 PMaarjav
12/11/2018, 10:51 PMcompute {something()} forCondition isReady
where compute is a function that takes in a lambda that returns an object with a forCondition infix methodmiha-x64
12/12/2018, 9:16 AMMarc Knaup
12/12/2018, 9:20 AMaarjav
12/13/2018, 2:45 AM