Testing Scalez === with ScalaTest

93 views
Skip to first unread message

Shing Hing Man

unread,
Mar 24, 2016, 1:52:33 PM3/24/16
to scalaz
Hi,
   I am using ScalaZ 7.1.3.

I am trying to write a simple test case for Scalaz === with ScalaTest.


import org.scalactic.TripleEquals.{convertToEqualizer => _}
import scalaz.Scalaz._
import scalaz._
import org.scalatest.{Matchers, WordSpec}

class TypeClasses2Test extends WordSpec with Matchers{


  "Strings" should {
    "be equals"  in{


       "Hello" === "olleH".reverse should be (true)

    }
  }
}

Error:(19, 8) type mismatch;
 found   : String("Hello")
 required: ?{def ===(x$1: ? >: String): ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method ToEqualOps in trait ToEqualOps of type [F](v: F)(implicit F0: scalaz.Equal[F])scalaz.syntax.EqualOps[F]
 and method convertToEqualizer in trait TripleEquals of type [T](left: T)TypeClasses2Test.this.Equalizer[T]
 are possible conversion functions from String("Hello") to ?{def ===(x$1: ? >: String): ?}
       "Hello" === "olleH".reverse should be (true)
       ^


There is another implicit conversion  for === from org.scalactic.TripleEquals.convertToEqualizer, which I tried to exclude.
But
import org.scalactic.TripleEquals.{convertToEqualizer => _}

does not seem to work.

It would be appreciated if someone can give me a pointer on  how to get the test case working.

Thanks in advance for any assistance !

Shing



Stephen Compall

unread,
Mar 30, 2016, 3:43:11 PM3/30/16
to sca...@googlegroups.com
On 2016-03-24 10:52 AM, 'Shing Hing Man' via scalaz wrote:
class TypeClasses2Test extends WordSpec with Matchers{
<snip>

Note that implicit conversions are not applicable because they are ambiguous:
 both method ToEqualOps in trait ToEqualOps of type [F](v: F)(implicit F0: scalaz.Equal[F])scalaz.syntax.EqualOps[F]
 and method convertToEqualizer in trait TripleEquals of type [T](left: T)TypeClasses2Test.this.Equalizer[T]

This part of the error indicates that you are getting this implicit conversion from one of the traits you are mixing into TypeClasses2Test; you can't fix this by changing imports.  Check the superclasses of Matchers and WordSpec in Scaladoc, and you'll find this TripleEquals trait mentioned by the error.

You might be able to suppress the conversion by adding to your TypeClasses2Test, or making the last mixin

  override def convertToEqualizer // same tparams, params, return type...
    = super.convertToEqualizer(/* pass args */)

In general, I strongly advise against the "mixin" style of importing when designing your own libraries; it is very inflexible, type-level-complex, and makes maintenance harder.

-- 
Stephen Compall
If anyone in the MSA is online, you should watch this flythrough.

Shing Hing Man

unread,
Mar 31, 2016, 3:37:34 PM3/31/16
to scalaz

 Thanks for the suggestion and it works!

Shing

Sean McLaughlin

unread,
Mar 7, 2017, 2:47:12 PM3/7/17
to scalaz
If you're OK with unicode, you can also use ≟ instead of ===.

Stephen Compall

unread,
Mar 9, 2017, 7:41:31 PM3/9/17
to sca...@googlegroups.com
It doesn't work because the actual conversion being applied is this.convertToEqualizer, not the globally-accessible one.  Try this goofiness instead, in your class body:

  override def convertToEqualizer[T](left: T): Equalizer[T] =
    super.convertToEqualizer(left)

Nonsense like this?  Just another day living in the cake.

--
Stephen Compall

Reply all
Reply to author
Forward
0 new messages