Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Slick require using three equal signs (===) for comparison?

Tags:

scala

slick

I was reading through coming from SQL to Slick and it states to use === instead of == for comparison.

For example,

people.filter(p => p.age >= 18 && p.name === "C. Vogt").run

What is the difference between == and ===, and why is the latter used here?

like image 714
Kat Avatar asked Oct 04 '14 13:10

Kat


People also ask

Why does JavaScript use 3 equal signs?

The triple equals sign in JavaScript means “equality without type coersion”. That means, the type and values must both be equal. Take for example the scenario where 0 is false. If we compare the same 0 and false with ===, we have false returned.

What does 3 equal signs mean in coding?

Triple equal sign in javascript means equality without type coercion. For example: 1=="1" // true, automatic type coersion 1==="1" // false, not the same type. Follow this answer to receive notifications.

What does === mean in typescript?

The Typescript has two operators for checking equality. One is == (equality operator or loose equality operator) and the other one is === (strict equality operator). Both of these operators check the value of operands for equality.

What does the === comparison operator do?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What does === mean in angular?

its javascript not angularjs, === is strict check. it checks data types and values.

When would you use the === identity operator?

The comparison operator called as the Identical operator is the triple equal sign “===”. This operator allows for a much stricter comparison between the given variables or values. This operator returns true if both variable contains same information and same data types otherwise return false.


2 Answers

== calls for equals, === is a custom defined method in slick which is used for column comparison:

def === [P2, R](e: Column[P2])(implicit om: o#arg[B1, P2]#to[Boolean, R]) =
  om.column(Library.==, n, e.toNode)

The problem of using == for objects is this (from this question):

Default implementation of equals() class provided by java.lang.Object compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object.

What this means is that two variables must point to the same object to be equal, example:

scala> class A
defined class A

scala> new A
res0: A = A@4e931efa

scala> new A
res1: A = A@465670b4

scala> res0 == res1
res2: Boolean = false

scala> val res2 = res0
res2: A = A@4e931efa

scala> res2 == res0
res4: Boolean = true

In the first case == returns false because res0 and res1 point to two different objects, in the second case res2 is equal to res0 because they point to the same object.

In Slick columns are abstracted in objects, so having column1 == column2 is not what you are looking for, you want to check equality for the value a column hold and not if they point to the same object. Slick then probably translates that === in a value equality in the AST (Library.== is a SqlOperator("="), n is the left hand side column and e the right hand side), but Christopher can explain that better than me.

like image 190
Ende Neu Avatar answered Oct 01 '22 14:10

Ende Neu


== is defined on Any in Scala. Slick can't overload it for Column[...] types like it can for other operators. That's why slick needs a custom operator for equality. We chose === just like several other libraries, such as scalatest, scalaz, etc.

a == b will lead to true or false. It's a client-side comparison. a === b will lead to an object of type Column[Boolean], with an instance of Library.Equals(a,b) behind it, which Slick will compile to a server-side comparison using the SQL "a = b" (where a and b are replaced by the expressions a and b stand for).

like image 43
cvogt Avatar answered Oct 01 '22 13:10

cvogt