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?
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.
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.
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.
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.
its javascript not angularjs, === is strict check. it checks data types and values.
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.
==
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.
==
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With