小编tsj*_*nsn的帖子

在Scala中生成格式化的XML

我有一些使用嵌入式Scala生成的XML,但它并没有将生成的XML放在不同的行上.

目前,它看起来像这样,

<book id="0">
      <author>Gambardella, Matthew</author><publish_date>Sun Oct 01 00:00:00 EDT 2000</publish_date><description>An in-depth loo
k at creating applications with XML.</description><price>44.95</price><genre>Computer</genre><title>XML Developer's Guide</title>
    </book>
Run Code Online (Sandbox Code Playgroud)

但我希望它看起来像这样:

<book id="0">
  <author>Gambardella, Matthew</author>
  <publish_date>Sun Oct 01 00:00:00 EDT 2000</publish_date>
  <description>An in-depth look at creating applications with XML.</description>
  <price>44.95</price>
  <genre>Computer</genre>
  <title>XML Developer's Guide</title>
</book>
Run Code Online (Sandbox Code Playgroud)

如何控制格式?这是生成XML的代码

<book id="0">
  { keys map (_.toXML) }
</book>
Run Code Online (Sandbox Code Playgroud)

这是toXML:

def toXML:Node = XML.loadString(String.format("<%s>%s</%s>", tag, value.toString, tag))
Run Code Online (Sandbox Code Playgroud)

xml formatting scala

9
推荐指数
1
解决办法
4840
查看次数

SBT,println和scala控制台应用程序的奇怪问题

当我运行我的scala代码(我正在使用SBT)时,在输入一些文本后会显示提示,如下所示:

C:\... > sbt run
[info] Loading project definition [...]
[info] Set current project to [...]
Running com[...]
test
>>





exit
>> >> >> >> >> >> [success] Total time[...]
Run Code Online (Sandbox Code Playgroud)

看起来它正在堆叠print()语句,只在运行不同的命令时才显示它们.

如果我使用println()它可以正常工作(除了我不想要换行)

代码:

...
  def main(args:Array[String]) {
    var endSession:Boolean = false
    var cmd = ""
    def acceptInput:Any = {
      print(">> ")
      cmd = Console.readLine
      if (cmd != "exit") {
        if (cmd != "") runCommand(cmd)
        acceptInput
      }
    }

    acceptInput
  }
...
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

io scala console-application sbt

3
推荐指数
1
解决办法
2282
查看次数

有没有办法隐含地完成这项工作?

有没有办法在匹配之前调用x上的这个隐式方法来满足匹配的类型要求?

如果我直接调用它,它按预期工作,但我想知道是否可以推断出该调用.

object ImplicitTest extends App {
  implicit def denull[T<:Any](mightBeNull:T):Option[T] = {
    if (mightBeNull == null) None
    else Some(canBeNull)
  }

  var x:String = null
  x match {  //works if i do "denull(x) match {"
    case Some(str:String) =>
      println(str)
    case None => None
  }
}
Run Code Online (Sandbox Code Playgroud)

scala type-inference implicit

1
推荐指数
1
解决办法
112
查看次数