score:7

Accepted answer

since the inheritance chain set <: genset <: gensetlike is a bit lengthy, it might be not immediately obvious where to look for the code of equals, so i thought maybe i quote it here:

gensetlike.scala:

  /** compares this set with another object for equality.
   *
   *  '''note:''' this operation contains an unchecked cast: if `that`
   *        is a set, it will assume with an unchecked cast
   *        that it has the same element type as this set.
   *        any subsequent classcastexception is treated as a `false` result.
   *  @param that the other object
   *  @return     `true` if `that` is a set which contains the same elements
   *              as this set.
   */
  override def equals(that: any): boolean = that match {
    case that: genset[_] =>
      (this eq that) ||
      (that canequal this) &&
      (this.size == that.size) &&
      (try this subsetof that.asinstanceof[genset[a]]
       catch { case ex: classcastexception => false })
    case _ =>
      false
  }

essentially, it checks whether the other object is also a genset, and if yes, it attempts to perform some fail-fast checks (like comparing size and invoking canequal), and if the sizes are equal, it checks whether this set is a subset of another set, presumably by checking each element.

so, the exact class used to represent the set at runtime is irrelevant, what matters is that the compared object is also a genset and has the same elements.

score:3

scala 2.12.8 documentation:

this class implements immutable sets using a list-based data structure.

so listset is a set too but with concrete (list-based) implementation.

score:4

from scala collections equality:

the collection libraries have a uniform approach to equality and hashing. the idea is, first, to divide collections into sets, maps, and sequences.

...

on the other hand, within the same category, collections are equal if and only if they have the same elements

in your case, both collections are considered sets and they contain the same elements, hence, they're equal.


Related Query

More Query from same tag