Correct syntax for not operator

142 views
Skip to first unread message

des.m...@gmail.com

unread,
Mar 13, 2014, 7:12:51 AM3/13/14
to squ...@googlegroups.com
Hi

If I define a table:

class A(f1: Int, f2: Boolean)
val as = table[A]

and want to select all entries which have f2 false I try the following

as.where(a => not(a.f2))

but this gives me a compiler type mismatch. Boolean required: LogicalBoolean

What is the correct syntax for using the not clause?

Thanks
Des

Vasya Novikov

unread,
Mar 13, 2014, 9:23:47 AM3/13/14
to squ...@googlegroups.com
do you have a `not` function defined somewhere else in scope?

The squeryl's `not` would only allow LogicalBoolean as input (argument).

Anyway, you can use `a.f2 === false` if you wish.
> --
> You received this message because you are subscribed to the Google
> Groups "Squeryl" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to squeryl+u...@googlegroups.com
> <mailto:squeryl+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

voro...@gmail.com

unread,
Mar 13, 2014, 3:06:09 PM3/13/14
to squ...@googlegroups.com, n1m5-goo...@yandex.ru
Another option would be to put in scope implicit conversion from Booleans and Boolea-Typed expressions:

  def testBool = {
    implicit def allowBooleanTE(te: org.squeryl.dsl.TypedExpression[Boolean, org.squeryl.dsl.TBoolean]): org.squeryl.dsl.ast.LogicalBoolean = new org.squeryl.dsl.ast.PostfixOperatorNode("", te) with org.squeryl.dsl.ast.LogicalBoolean
    implicit def allowBoolean(e: Boolean): org.squeryl.dsl.ast.LogicalBoolean = new org.squeryl.dsl.ast.PostfixOperatorNode("", booleanToTE(e)) with org.squeryl.dsl.ast.LogicalBoolean

    inTransaction {
      val q = from(ArticlesSchema.articles)(s => where(s.ready) select(s))
      println(q.statement)
      val q2 = from(ArticlesSchema.articles)(s => where(not(s.ready)) select(s))
      println(q2.statement)
    }
  }

Where parts of the two statements will be:
Where Article4.ready
Where not(Article4.ready )

Extra space in the "not" case is from me lazily reusing PostfixOperatorNode, while the proper solution would be to introduce BooleanOperatorNode as something like this:
class BooleanOperatorNode(val arg: ExpressionNode) extends ExpressionNode with org.squeryl.dsl.ast.LogicalBoolean {

  def doWrite(sw: StatementWriter) = {
    arg.write(sw)
  }

  override def children = List(arg)
}

But that would need some crafty implicit to type check that the passed-in expression is of type boolean. Which I can not write from the top of my head.

Qestion to Squeryl authors: do you want this implicits included in Squeryl? If so, I could work on the type-checking implicit and send a pull request.

-- Aleksey

voro...@gmail.com

unread,
Mar 13, 2014, 3:08:08 PM3/13/14
to squ...@googlegroups.com, n1m5-goo...@yandex.ru

voro...@gmail.com

unread,
Mar 13, 2014, 3:34:59 PM3/13/14
to squ...@googlegroups.com
Note to TC: you can also achieve the same with:
as.where(a => a.f2 === false)
or
as.where(a => a.f2 <> true)

David Whittaker

unread,
Mar 13, 2014, 7:28:37 PM3/13/14
to squ...@googlegroups.com
On Thu, Mar 13, 2014 at 3:06 PM, <voro...@gmail.com> wrote:
Another option would be to put in scope implicit conversion from Booleans and Boolea-Typed expressions:

This implicit used to exist as part of Squeryl and it led to a lot of inadvertent user errors.  I don't remember the specifics off the top of my head, but you could search the group for more info.  In general, I think it's a bad idea and your suggestion of a.f2 === false is the way to go.
 
To unsubscribe from this group and stop receiving emails from it, send an email to squeryl+u...@googlegroups.com.

voro...@gmail.com

unread,
Mar 13, 2014, 7:58:16 PM3/13/14
to squ...@googlegroups.com
After thinking what bugs that could cause, I agree. If someone types where(a.id == 1) now it would fail to compile and rightfully so. Introducing the implicits I wrote would make the code correct, compile into something user never wanted, hide the error and send a user in the wrong direction.

Thanks for the answer.
Reply all
Reply to author
Forward
0 new messages