7

I'm trying to check if some web page is up using the following function:

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

The function getAsString return type is net.liftweb.common.Box[String]

The function works just fine but my problem is that when I replace line 6 with this line:

       case Failure => false                                        // 6

I'm getting the error:

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

(same thing true for line 5)

Why does it happen? Why do I have to use a variable for the match and can't do the match according to the type only?

4

1 に答える 1

6

You can't match like that based on type, if you use Failure as a pattern you must match on a construuctor:

case Failure(_, _, _) => false
于 2012-11-07T13:19:03.837 に答える