Ian
Ian

Reputation: 6154

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

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?

Upvotes: 1

Views: 145

Answers (2)

Jasper-M
Jasper-M

Reputation: 15086

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.

Upvotes: 5

FunProg
FunProg

Reputation: 140

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).

Upvotes: 3

Related Questions