How to check equality of Array[Array[...]]

Neither do I. m( Somehow I was convinced that I’d seen #arr being a def rather than a val, and I haven’t cross-checked before replying. My bad, sorry for the noise.

1 Like

Bit late but I couldn’t find better answer, so I leave my best one just for record. We can create extension method in scala 3 and it will select proper version of it.


extension [T](of: Array[Array[T]])
  def deepSimilar(to: Array[Array[T]]) = 
    of.corresponds(to)(_ sameElements _)

extension [T](of: Array[Array[Array[T]]])
  def deepSimilar(to: Array[Array[Array[T]]]) =
    of.corresponds(to)((of, to) => of.corresponds(to)(_ sameElements _))

extension [T](of: Array[Array[Array[Array[T]]]])
  def deepSimilar(to: Array[Array[Array[Array[T]]]]) =
    of.corresponds(to)((of, to) =>
      of.corresponds(to)((of, to) => of.corresponds(to)(_ sameElements _))
    )

// should all be true
Array(Array(1)) deepSimilar Array(Array(1))
Array(Array(Array(1))) deepSimilar Array(Array(Array(1)))
Array(Array(Array(Array(1)))) deepSimilar Array(Array(Array(Array(1))))

// should all be false
Array(Array(2)) deepSimilar Array(Array(1))
Array(Array(Array(2))) deepSimilar Array(Array(Array(1)))
Array(Array(Array(Array(2)))) deepSimilar Array(Array(Array(Array(1))))

//this case has no extension method so deepest Array is compered using `==`
//and it will fail even ifit is not desired

Array(Array(Array(Array(Array(1))))) deepSimilar Array(
  Array(Array(Array(Array(1))))
)

// should not compile?
// Array(Array(2)) deepSimilar Array(Array("test"))
// Array(Array(Array(2))) deepSimilar Array(Array(Array("test")))
// Array(Array(Array(Array(2)))) deepSimilar Array(Array(Array(Array("test"))))
// Array(Array(2)) deepSimilar Array(2)
// Array(Array(Array(2))) deepSimilar Array(Array(2))
// Array(Array(Array(Array(2)))) deepSimilar Array(Array(Array(2)))