-1

Given the following class:

scala> class Foo(x: Int) { def getX = x }
defined class Foo

I created an implicit Equal[Foo] to be able to use ===.

scala> implicit val FooEq: Equal[Foo] = Equal.equal(_.getX == _.getX)
FooEq: scalaz.Equal[Foo] = scalaz.Equal$$anon$7@6a246ad

It works.

scala> new Foo(10) === new Foo(10)
res2: Boolean = true

scala> new Foo(10) === new Foo(4545)
res3: Boolean = false

But, I'm confused by how the the FooEq gets created.

What's going on in Equal.equal(_.getX == _.getX)? I'm not sure how that statement returns an Equal[Foo].

Noah
  • 13,821
  • 4
  • 36
  • 45
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

1

The type signature of Equal.equal is def equal[A](f: (A, A) => Boolean): Equal[A]. So you're passing a method that takes two Foos and gives back a Boolean to equals which gives back an instance of Equal[Foo]. That's whats going on and that's how the statement returns and Equal[Foo].

Here's some code that you've written using similar syntax.

Noah
  • 13,821
  • 4
  • 36
  • 45