Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Op.build can't chain?

Open wgq91here opened this issue 4 years ago • 2 comments

Like this codes:

it can update.

return Op.build { Members.id greaterEq 0 }.let {
          var o = it
          if (query.id != null) {
              o = o.and {
                  Members.id eq query.id
              }
          }
         o
}

but, it can't be updated.

return Op.build { Members.id greaterEq 0 }.let {
    var o = it
    if (query.id != null) {
        o.and {
            Members.id eq query.id
        }
    }
   o
}

Condition is True in above codes. But return value is different. Why?

And Apply not be useful.

wgq91here avatar Apr 29 '22 07:04 wgq91here

o.and { Members.id eq query.id } - return new object

SchweinchenFuntik avatar Jun 10 '22 12:06 SchweinchenFuntik

val other = o.and { Members.id eq query.id  }
println(other == o)
println(o)
println(other)

SchweinchenFuntik avatar Jun 10 '22 12:06 SchweinchenFuntik

As detailed above, the operator and() (as well as or() and both their variants) returns a new Op<Boolean> object. It does not mutate the object that invokes it.

Apologies if your use case is not relevant to this suggestion, but if you are interested in mutating, for example, an existing Query, then andWhere() could be an option. Something like this:

val ogQuery = Members.select { Members.id greaterEq 0 }
query.id?.let {
    ogQuery.andWhere { Members.id eq it }
}

Please consider reopening this issue on YouTrack if the problem requires more answers.

bog-walk avatar Oct 18 '23 18:10 bog-walk