You should use foldLeft
instead of fold
:
str
.foldLeft(""){ (x, y) => if (!(x contains y)) x + y else x }
.size == str.size
the signature for fold
is:
fold[A1 >: Char](z: A1)(op: (A1, A1) ⇒ A1): A1
So fold
expects the element type to be a lower bound on the accumulator type. The element type is Char
in the case of String
, so the possible types for A1
are Char
, AnyVal
and Any
. You pass a String
so Any
is the only common type, which means x
and y
have type Any
, which does not have a contains
method.
You need to the accumulator type to be different from the element type so you need to use foldLeft
.