Gandalf
Gandalf

Reputation: 166

Condition as function in for comprehension loop

I would like to use a custom condition function in my for-comprehension loop for slick query:

def findNewsletterUsers(f: (NewsletterUsers, NewsletterStatuses) => Boolean) = {
    for {
      u <- NewsletterUsers
      s <- NewsletterStatuses if (u.statusId === s.id)
      d <- NewsletterDelays if (u.delayId === d.id)
      if f(u, s)
    } yield (u, d, s)
  }

I would like to invoke this method with actual custom condition filled in a layer above. Something like:

findNewsletterUsers((nu, ns) => ns.name == 'C')

This invocation returns no rows though (while it should). Is there any way to make it work that way?

Best regards

Upvotes: 0

Views: 190

Answers (1)

Wellingr
Wellingr

Reputation: 1191

An easy mistake to make in slick is to use the == operator instead of the === operator.

In your case you use the function (nu, ns) => ns.name == 'C'). This function always returns false because ns.name is not a Char but a Rep[Char].

To solve this issue, you simply need to replace == by ===, slick will automatically lift the literal char 'C' to a Rep[Char].

Upvotes: 1

Related Questions