加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala Liftweb – 模式类型与预期类型不兼容

发布时间:2020-12-16 19:16:32 所属栏目:安全 来源:网络整理
导读:我正在尝试使用以下函数检查某个网页是否已启动: import net.liftweb.common.{Failure,Empty,Full,Box} // 1 // 2def isAlive = { // 3 httpClient.getAsString("http://www.google.com",Nil) match { // 4 case f : Full[String] = true // 5 case f : Fai
我正在尝试使用以下函数检查某个网页是否已启动:

import net.liftweb.common.{Failure,Empty,Full,Box}               // 1
                                                                    // 2
def isAlive = {                                                     // 3
    httpClient.getAsString("http://www.google.com",Nil) match {    // 4
       case f : Full[String] => true                                // 5
       case f : Failure => false                                    // 6
       case Empty => false                                          // 7
    }                                                               // 8
}                                                                   // 9

函数getAsString的返回类型是net.liftweb.common.Box [String]

该功能工作正常但我的问题是,当我用这一行替换第6行:

case Failure => false                                        // 6

我收到了错误:

error: pattern type is incompatible with expected type;
found   : object net.liftweb.common.Failure
required: net.liftweb.common.Box[String]
case Failure => false

(第5行也是如此)

为什么会这样?为什么我必须使用变量进行匹配,并且不能仅根据类型进行匹配?

解决方法

您不能像基于类型那样匹配,如果您将Failure用作模式,则必须在构造函数上匹配:

case Failure(_,_,_) => false

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读