When I try to do this in Scala 3.2.0:
// CellTower from Google Maps API class unfortunately has no equals()
// implementation, but a toString(), so we use this for equality
def isEqualTo(cellTowers: Seq[CellTower], others: Seq[CellTower]): Boolean =
cellTowers.zip(others).fold(true)((c1, c2) => c1.toString == c2.toString)
I get:
Found: Matchable
Required: Boolean
cellTowers.zip(others).fold(true)((c1, c2) => c1.toString == c2.toString)
I read https://docs.scala-lang.org/scala3/reference/other-new-features/matchable.html, but I still don't understand the problem (and cannot create a solution)
CodePudding user response:
It seems that you probably wanted to fold all the booleans with &&
, but somehow forgot the actual folding operation:
def isEqualTo(cellTowers: Seq[CellTower], others: Seq[CellTower]): Boolean =
cellTowers.zip(others).foldLeft(true){ case (b, (c1, c2)) => b && (c1.toString == c2.toString) }
This, however, would be much more easily expressed with forall
:
def isEqualTo(cellTowers: Seq[CellTower], others: Seq[CellTower]): Boolean =
cellTowers.zip(others).forall((c1, c2) => c1.toString == c2.toString)
(and it would also be faster, because it can return early in case it finds a false
).
This still doesn't help you with sequences of different length though, so you probably should compare the size first.