在 GitHub 上编辑此页面

已弃用:XML 字面量

XML 字面量仍然受支持,但将在不久的将来被删除,并由 XML 字符串插值 替换

import dotty.xml.interpolator.*

case class Person(name: String) { override def toString = name }

@main def test: Unit =
  val bill = Person("Bill")
  val john = Person("John")
  val mike = Person("Mike")
  val todoList = List(
    (bill, john, "Meeting", "Room 203, 11:00am"),
    (john, mike, "Holiday", "March 22-24")
  )
  // XML literals (to be dropped)
  val mails1 = for (from, to, heading, body) <- todoList yield
    <message>
      <from>{from}</from><to>{to}</to>
      <heading>{heading}</heading><body>{body}</body>
    </message>
  println(mails1)
  // XML string interpolation
  val mails2 = for (from, to, heading, body) <- todoList yield xml"""
    <message>
      <from>${from}</from><to>${to}</to>
      <heading>${heading}</heading><body>${body}</body>
    </message>"""
  println(mails2)

有关更多信息,请参阅 Yassin Kammoun (2019) 的学期项目 Dotty 的 XML 字符串插值