This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala's StringBuilder vs. Java's StringBuilder

4 replies
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.

Hi,

On the ScalaDoc page for scala.StringBuilder it says:

"A mutable sequence of characters. This class provides an API compatible
with java.lang.StringBuilder. "

But this is not really true. In particular, java.lang.StringBuilder
implements java.lang.Appendable and there is no counterpart in
scala.StringBuilder. Hence APIs that specify a java.lang.Appendable
cannot be passed a scala.StringBuilder.

Randall Schulz

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala's StringBuilder vs. Java's StringBuilder


On Mon, Mar 23, 2009 at 8:10 PM, Randall R Schulz <rschulz@sonic.net> wrote:
Hi,

On the ScalaDoc page for scala.StringBuilder it says:

"A mutable sequence of characters. This class provides an API compatible
with  java.lang.StringBuilder. "

FWIW Appendable is a horrid interface in Java, making StringBuilder.append throw a cheked exception (IOException) is beyond retardation.
 


But this is not really true. In particular, java.lang.StringBuilder
implements java.lang.Appendable and there is no counterpart in
scala.StringBuilder. Hence APIs that specify a java.lang.Appendable
cannot be passed a scala.StringBuilder.


Randall Schulz



--
Viktor Klang
Senior Systems Analyst
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Scala's StringBuilder vs. Java's StringBuilder

On Monday March 23 2009, Viktor Klang wrote:
> On Mon, Mar 23, 2009 at 8:10 PM, Randall R Schulz wrote:
> > Hi,
> >
> > On the ScalaDoc page for scala.StringBuilder it says:
> >
> > "A mutable sequence of characters. This class provides an API
> > compatible with java.lang.StringBuilder. "
>
> FWIW Appendable is a horrid interface in Java, making
> StringBuilder.append throw a cheked exception (IOException) is beyond
> retardation.

Nonetheless, it unifies StringBuffer / StringBuilder and the stream /
writer classes, and for that reason I wish to use it.

Randall Schulz

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Scala's StringBuilder vs. Java's StringBuilder

On Monday March 23 2009, Viktor Klang wrote:
> On Mon, Mar 23, 2009 at 8:10 PM, Randall R Schulz
wrote:
> > Hi,
> >
> > On the ScalaDoc page for scala.StringBuilder it says:
> >
> > "A mutable sequence of characters. This class provides an API
> > compatible with java.lang.StringBuilder. "
>
> FWIW Appendable is a horrid interface in Java, making
> StringBuilder.append throw a cheked exception (IOException) is beyond
> retardation.

By the way, implementing classes can weaken a method interface contract
by not throwing an exception that is declared as thrown in an
overridden method. StringBuilder does indeed implement Appendable, but
its append methods (those that override Appendable's) do not throw
IOException (none do, in fact).

Randall Schulz

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala's StringBuilder vs. Java's StringBuilder
If it's important to have a Scala Appendable, of course in Scala writing a delegate is much more concise than in Java, because you can leave out type annotations, {} and ;. Also the Proxy trait takes care of equals etc. I think. class A(self: B) extends Proxy { def method = self.method; ... }

On Mon, Mar 23, 2009 at 5:28 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 23 2009, Viktor Klang wrote:
> On Mon, Mar 23, 2009 at 8:10 PM, Randall R Schulz <rschulz@sonic.net>
wrote:
> > Hi,
> >
> > On the ScalaDoc page for scala.StringBuilder it says:
> >
> > "A mutable sequence of characters. This class provides an API
> > compatible with  java.lang.StringBuilder. "
>
> FWIW Appendable is a horrid interface in Java, making
> StringBuilder.append throw a cheked exception (IOException) is beyond
> retardation.

By the way, implementing classes can weaken a method interface contract
by not throwing an exception that is declared as thrown in an
overridden method. StringBuilder does indeed implement Appendable, but
its append methods (those that override Appendable's) do not throw
IOException (none do, in fact).


Randall Schulz

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland