Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why there occur differences is documented in ScalaDoc.</p> <p>WrappedString:</p> <blockquote> <p>The difference between this class and <code>StringOps</code> is that calling transformer methods such as <code>filter</code> and <code>map</code> will yield an object of type <code>WrappedString</code> rather than a <code>String</code>.</p> </blockquote> <p>StringOps:</p> <blockquote> <p>The difference between this class and <code>WrappedString</code> is that calling transformer methods such as <code>filter</code> and <code>map</code> will yield a <code>String</code> object, whereas a <code>WrappedString</code> will remain a <code>WrappedString</code>.</p> </blockquote> <p>Both derive from <code>collection.GenSeqLike</code> which defines an equals method:</p> <pre><code>override def equals(that: Any): Boolean = that match { case that: GenSeq[_] =&gt; (that canEqual this) &amp;&amp; (this sameElements that) case _ =&gt; false } </code></pre> <p>Both implement the <code>canEqual</code> (derived from <code>collection.IterableLike</code>) which <a href="https://github.com/scala/scala/blob/v2.10.0-M4/src/library/scala/collection/IterableLike.scala#L292" rel="noreferrer">returns always true</a>. But <code>StringOps</code> is not a <code>collection.GenIterable</code>:</p> <pre><code>scala&gt; st1 sameElements st2 &lt;console&gt;:13: error: type mismatch; found : scala.collection.immutable.StringOps required: scala.collection.GenIterable[?] st1 sameElements st2 ^ </code></pre> <p>Whereas <code>WrappedString</code> does:</p> <pre><code>scala&gt; st2 sameElements st1 res13: Boolean = true </code></pre> <p>So it should obvious why the first case returns <code>true</code> and the other one <code>false</code>.</p> <p>But why do both exist? I'm not totally sure why it is designed this way, but I think that's because of the fact that a String is not a collection in Scala. When we do some operation on a String like <code>"abc" flatMap (_+"z")</code> we wanna get back another String even though it is not always possible as shown by <code>"abc" map (_+1)</code>. This is what <code>StringOps</code> does. But when we have some method <code>def x[A](s: Seq[A]) = s.getClass</code> how shall we call it with a String? In this case we need <code>WrappedString</code>:</p> <pre><code>scala&gt; x("a") res9: Class[_ &lt;: Seq[Char]] = class scala.collection.immutable.WrappedString </code></pre> <p>So, <code>StringOps</code> is more lightweight as <code>WrappedString</code>. It allows us to call some methods on plain old <code>java.lang.String</code> without doing too much overhead. In 2.10 <code>StringOps</code> extend <code>AnyVal</code>. This means it is a value class and its existence can be optimized by scalac (no runtime overhead any more by wrapping the String). In contrast <code>WrappedString</code> allows us to handle a String as a real collection - as an <code>IndexedSeq[Char]</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload