Scala Liftweb - pattern type is incompatible with expected type
Asked Answered
B

1

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?

Basile answered 7/11, 2012 at 13:12 Comment(0)
L
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
Largehearted answered 7/11, 2012 at 13:19 Comment(1)
Older scala compilers didn't check this. Then my error. ThxAmygdala

© 2022 - 2024 — McMap. All rights reserved.