scala 中一系列值的模式匹配

发布于 2024-08-12 05:07:48 字数 783 浏览 6 评论 0原文

我是 Scala 初学者,这段代码让我很挣扎。

有没有办法进行模式匹配以确保我传递给数据的所有内容都是正确的类型? 我有很奇怪的数据类型

class Data (
val recipient: String, 
val templateText: String, 
val templateHtml: String, 
val blockMaps: Map[String,List[Map[String,String]]], 
templateMap: Map[String,String]
)

......

val dataParsed = JSON.parseFull(message)
dataParsed match {
 case dataParsed: Map[String, Any] => {
  def e(s: String) = dataParsed get s
  val templateText = e("template-text")
  val templateHtml = e("template-html")
  val recipient = e("email")
  val templateMap = e("data")
  val blockMaps = e("blkdata")

  val dependencies = new Data(recipient, templateText, templateHtml, blockMaps, templateMap)
  Core.inject ! dependencies
 }

正如你所看到的,

I'm a Scala beginner and this piece of code makes me struggle.

Is there a way to do pattern matching to make sure everything i pass to Data is of the correct type? As you can see i have quite strange datatypes...

class Data (
val recipient: String, 
val templateText: String, 
val templateHtml: String, 
val blockMaps: Map[String,List[Map[String,String]]], 
templateMap: Map[String,String]
)

...

val dataParsed = JSON.parseFull(message)
dataParsed match {
 case dataParsed: Map[String, Any] => {
  def e(s: String) = dataParsed get s
  val templateText = e("template-text")
  val templateHtml = e("template-html")
  val recipient = e("email")
  val templateMap = e("data")
  val blockMaps = e("blkdata")

  val dependencies = new Data(recipient, templateText, templateHtml, blockMaps, templateMap)
  Core.inject ! dependencies
 }

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-08-19 05:07:48

我猜你的问题是你希望能够匹配从 parseFull() 获得的地图,但 Map 没有 unapply

因此,您可以对每个值进行模式匹配,如果类型不正确,则提供默认值:

val templateText: Option[String] = e("template-text") match {
  case s: String => Some(s)
  case _ => None
}

或者暂时将所有数据放入可以进行模式匹配的某种结构中:

val data = (e("template-text"), e("template-html"), e("email"), e("data"),
            e("blkdata"))

val dependencies: Option[Data] = data match {
  case (templateText: String,
        templateHtml: String,
        blockMaps: Map[String,List[Map[String,String]]],
        templateMap: Map[String,String]) =>
    Some(new Data(recipient, templateText, templateHtml, blockMaps, templateMap))
  case _ => None
}

I guess your problem is you want to be able to patten match the map that you get from parseFull(), but Map doesn't have an unapply.

So you could pattern match every single value, providing a default if it is not of the correct type:

val templateText: Option[String] = e("template-text") match {
  case s: String => Some(s)
  case _ => None
}

Or temporarily put all the data into some structure that can be pattern matched:

val data = (e("template-text"), e("template-html"), e("email"), e("data"),
            e("blkdata"))

val dependencies: Option[Data] = data match {
  case (templateText: String,
        templateHtml: String,
        blockMaps: Map[String,List[Map[String,String]]],
        templateMap: Map[String,String]) =>
    Some(new Data(recipient, templateText, templateHtml, blockMaps, templateMap))
  case _ => None
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文