https://kotlinlang.org logo
Title
m

Marc Knaup

10/04/2019, 7:26 AM
TIL:
someFuncReturningBool() && otherFuncReturningBool()
and
someFuncReturningBool() and otherFuncReturningBool()
is not the same. The former short-circuits, the latter doesn’t. That hit me unexpectedly when I thought “oh that’s nice, I can just use
and
instead of
&&
in my DSL to make it more human-readable”.
rule {
   condition {
      terms.assignmentAddress.isProvided() and
         terms.assignmentAddress.country.isNotSupported() // exception because access not allowed if isProvided() returns false
   }

   issue {
      "Terms: The address of the assignment is located in a country that is not supported."
   }
}
I presume there are only two ways to fix this. a) Use
&&
instead of
and
😥 b) Make all the functions which can be used in
condition {}
that currently return
Boolean
return something like
Condition
instead. That in turn would have an
and
infix operator which makes two
Condition
instances form a new
Condition
and thus allows for chaining with
and
. That would basically build a tree of
Condition
. I guess there are no alternatives for supporting
and
here?
e

efemoney

10/08/2019, 12:46 AM
You have to be careful,
and
is a bitwise operator not a boolean operator.
m

Marc Knaup

10/08/2019, 3:29 AM
I know. Yet it's defined on
Boolean
, hence the confusion :)