Home > database >  Why is Scala's Set.subsets() method defined as an empty-paren method?
Why is Scala's Set.subsets() method defined as an empty-paren method?

Time:09-22

Scala's immutable Set class has a method called subsets() that returns all the subsets of a set.

Usually empty-paren methods are reserved for methods that cause side effects, but this one doesn't seem to.

Why isn't this method defined as a parameterless method? Does it in fact cause some side effect?

CodePudding user response:

https://github.com/scala/bug/issues/9116

Because subsets is overloaded. There used to be a type inference issue if one overload has no parens. I don't know if that type inference issue still exists, but the parens which were added as a workaround are still there.

CodePudding user response:

Because they return a mutable iterator to the set of subsets, so processing the iterator will have side-effects. Each call to subsets() will therefore also create a different iterator, so

someSet.subsets() != someSet.subsets()

which suggests that the call is not referentially transparent.

See also this discussion about a similar method (iterator()) on another collection type (IterableOnce).

  • Related