Search code examples
scalafunctional-programmingcomparison-operators

What does the symbol =?= mean in Scala?


Can somebody offer some insight into what this symbol means and how it is used in Scala? I first saw this symbol in this video about Functional Structure in Scala (You can see him typing the symbol here: https://youtu.be/tD_EyIKqqCk?t=3364)

A google search came up with this link to an outdated HTCondor manual in section 4.1.3.3 / Comparison operators / point 5 (https://research.cs.wisc.edu/htcondor/manual/v7.8/4_1HTCondor_s_ClassAd.html#ClassAd:evaluation-meta)

It states: "In addition, the operators =?= and =!= behave similar to == and !=, but are not strict. Semantically, the =?= tests if its operands are identical,'' i.e., have the same type and the same value. For example, 10 == UNDEFINED and UNDEFINED == UNDEFINED both evaluate to UNDEFINED, but 10 =?= UNDEFINED and UNDEFINED =?= UNDEFINED evaluate to FALSE and TRUE respectively. The =!= operator test for the is not identical to'' condition."

My question is, would using '==' give the same results in the code shown?


Solution

  • The quote you have is from some language ClassAd that does not seem to be related to Scala. I've never seen that method =?= before.

    However, I found the definition of it here, in a repository owned by Michael Pilquist, who also made the video:

    object IsEq {
      implicit class Syntax[A](lhs: A) {
        def =?=(rhs: A): IsEq[A] = IsEq(lhs, rhs)
      }
    }
    

    IsEq is defined as such:

    case class IsEq[A](lhs: A, rhs: A) {
      def isEqual(implicit eq: Equal[A]): Boolean = eq.isEqual(lhs, rhs)
    }
    

    And Equal seems to be a typeclass for restricting equality to objects of the same type and using custom behavior for comparison (e.g. there is an Equal instance for List that checks all the elements are equal).

    trait Equal[A] {
      def isEqual(lhs: A, rhs: A): Boolean
    }