Scala StringBuilder

南笙酒味 提交于 2019-12-05 00:58:15

You can't start with a Scala StringBuilder and then obtain the Java version. You can, however, wrap a java.lang.StringBuilder in the Scala version. So:

val jsb = new java.lang.StringBuilder();
val sb = new StringBuilder(jsb);
// Do Scala-y stuff with sb
JCommander.whatever.usage(jsb);
// Do more Scala-y stuff

Since--not a guarantee, but true in practice right now (2.8, 2.9)--the Scala wrapper doesn't store any state (instead just referring to the Java version), you're safe to mix and match usage of the two.

Looking at the source code, it seems there's absolutely no way to get a java.lang.StringBuilder from a scala.collection.mutable.StringBuilder.

You can create a new java.lang.StringBuilder with Scala's, going through a conversion of some type (String or CharSequence).

Use this implicit transform do do this:

    implicit def b2b(b: scala.collection.mutable.StringBuilder) = 
        new java.lang.StringBuilder(b)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!