poohbar
09/10/2018, 1:37 PMcheck
that would take a supplier lambda for the case when the check passes?Andreas Sinz
09/10/2018, 1:43 PMNikky
09/10/2018, 2:01 PMsomething.takeIf { check(it) }?.run(lambda) ?: throw Exception()
Nikky
09/10/2018, 2:01 PMpoohbar
09/10/2018, 2:30 PMcheck()
does not return BooleanAndreas Sinz
09/10/2018, 2:34 PMinline fun checkOnSuccess(value: Boolean, onSuccess: () -> Unit) {
if(value)
onSuccess()
else
throw IllegalStateException()
}
Pavlo Liapota
09/10/2018, 3:07 PMcheck(value)
onSuccess()
Everything after check()
is executed only if value
is true
, otherwise exception is thrown.Ruckus
09/10/2018, 3:57 PMcheck
(i.e. continue on true, throw on false), what @Pavlo Liapota suggested should do the trick. If you just want to return a Boolean
, you're already passing in a Boolean
so what does a new function get you over if
?poohbar
09/10/2018, 3:59 PMpoohbar
09/10/2018, 3:59 PMpoohbar
09/10/2018, 4:00 PMfun foo() = if (condition) value else throw IllegalStateException()
poohbar
09/10/2018, 4:00 PMcheck()
insteadRuckus
09/10/2018, 4:00 PMpoohbar
09/10/2018, 4:01 PMpoohbar
09/10/2018, 4:02 PMpoohbar
09/10/2018, 4:02 PMval value: Type = initialValue
get() {
// do something here
}
this just looks odd to me.. too much indentationRuckus
09/10/2018, 4:03 PMRuckus
09/10/2018, 4:03 PMfield
Ruckus
09/10/2018, 4:05 PMfield
. Personally I think it would make far more sense in the setter to use the properties name as the backing field, and if you do want to call the setter or getter recursively, use this.<propertyName>
.Ruckus
09/10/2018, 4:05 PMpoohbar
09/10/2018, 5:15 PM