- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala's StringBuilder vs. Java's StringBuilder
Mon, 2009-03-23, 20:10
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
Mon, 2009-03-23, 22:27
#2
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
Mon, 2009-03-23, 22:47
#3
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
Mon, 2009-03-23, 23:07
#4
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 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
On Mon, Mar 23, 2009 at 8:10 PM, Randall R Schulz <rschulz@sonic.net> wrote:
FWIW Appendable is a horrid interface in Java, making StringBuilder.append throw a cheked exception (IOException) is beyond retardation.
--
Viktor Klang
Senior Systems Analyst