Is isDefined needed for Option?

65 views
Skip to first unread message

Andrew Gaydenko

unread,
Nov 29, 2015, 7:22:11 AM11/29/15
to Slick / ScalaQuery
Hi! Say, some field is defined as Option[String]. Is there any difference between filters
{ f => f.field.isDefined && f.field === "some literal" }
and
{ f => f.field === "some literal" }
?

Toby Thain

unread,
Nov 30, 2015, 11:50:21 AM11/30/15
to Slick / ScalaQuery
You probably want .exists( predicate )

Andrew Gaydenko

unread,
Nov 30, 2015, 12:02:48 PM11/30/15
to Slick / ScalaQuery
On Monday, November 30, 2015 at 7:50:21 PM UTC+3, Toby Thain wrote:
You probably want .exists( predicate )

Yes, exists can be chained in query, but the question is if we need isDefined at all at those cases when the same optional field is compared to constant.

Toby Thain

unread,
Dec 1, 2015, 5:30:01 PM12/1/15
to Slick / ScalaQuery


On Sunday, 29 November 2015 07:22:11 UTC-5, Andrew Gaydenko wrote:
Ah right, my .exists suggestion wasn't thinking of the embedding language.

In the embedded query, you would need to follow the same rules as SQL, as far as I know. Which means that if you are doing an equality test, as in your example, NULL will behave as false, and no extra test is needed. For clarity you could decide to include it, though.

But if you are doing an inequality test, you would need the equivalent of 
  x IS NULL OR x <> literal
i.e. 
  x.isEmpty || x =!= literal

Because, as in SQL, a NULL would result in the inequality appearing false, when it should make the inequality true.

--Toby

Andrew Gaydenko

unread,
Dec 2, 2015, 2:09:57 AM12/2/15
to Slick / ScalaQuery
Toby, got it, thanks!
Reply all
Reply to author
Forward
0 new messages