Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:39+00:00 2026-05-23T15:24:39+00:00

PartialFunctions In Scala, a PartialFunction is, in short, a function that additionally defines an

  • 0

PartialFunctions

In Scala, a PartialFunction is, in short, a function that additionally defines an isDefinedAt method.

It is easy to define partial functions with a series of case statement. A trivial example would be, e.g.:

scala> val pf: PartialFunction[Int, Unit] = {
     | case 42 => ()
     | }
pf: PartialFunction[Int,Unit] = <function1>

scala> pf.isDefinedAt(42)
res0: Boolean = true

scala> pf.isDefinedAt(0) 
res1: Boolean = false

isDefinedAt is automatically generated from the list of cases defining the partial function.

Context

The Lift framework makes use of partial functions in many places, e.g. to define whether a request should be processed by Lift’s engine or served directly from a file on disk, as is. and sometimes, I find myself wanting to write a case statement that matches all input parameters and only later decide if I want to return a value or not. This means that the initial series of cases is not enough any more to determine if my function is defined at a given value or not

For instance, in Lift, I want to add a rule that all html and htm files are served directly, and that files with the “lift” extension should be processed. It would look easy to do something like this:

LiftRules.liftRequest.prepend {
  case Req(path, extension, tpe) => extension match {
    case "html" | "htm" => false
    case "lift" => true
  }
}

Unfortunately, in this case, the compiler thinks that my partial function is defined everywhere, as the first case always matches. It’s the nested match that may not match all incoming requests. And, is a request is not matched, a MatchError is thrown.

Question

Is there a simple way to make the compiler consider nested match statements when defining a partial function, or is the only way to do it to inline all nested conditionals like this?

LiftRules.liftRequest.prepend {
  case Req(path, extension, tpe) if extension == "html" || extension == "htm" => false
  case Req(path, extension, tpe) if extension == "lift" => true
}

In this example, it’s largely doable, but the readability is decreased, and I’ve faced cases where inlining all checks looks very ugly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:24:40+00:00Added an answer on May 23, 2026 at 3:24 pm

    In this case, you may want to write

    LiftRules.liftRequest.prepend {
      case Req(path, "html" | "htm", tpe) => false
      case Req(path, "lift", tpe) => true
    }
    

    For more complicated cases, you’ll need to define your own extractor which you’ll have to use instead of a nested case statement.

    object CheckExtension {
      def unapply(ext: String) = ext match {
        case "lift" => Some(true)
        case "html" | "htm" => Some(false)
        case _ => None
      }
    }
    
    LiftRules.liftRequest.prepend {
      case Req(path, CheckExtension(valid), tpe) => valid
    }
    

    This will only match if your predefined unapply function returns Some and assign the value of Some to the free variable valid. If unapply returns None, no match is being generated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to design a couple of classes that inherit a partial function, but
While creating a map of String to partial functions I ran into unexpected behavior.
In scala I can write: val pf: PartialFunction[String, Unit] = {case s => println(s)}
I read this post on Dr. Dobb's about currying and partial functions in JavaScript.
Consider the following: scala> object Currency extends Enumeration { | type Currency = Value
As Moggi proposed 20 years ago, the effectful function space -> of languages like
Consider the following (simplified) example: abstract class Bar[T] { val f: PartialFunction[T, T] val
in scala play framework I seen this code: abstract class AnalyserInfo case class ColumnC(typeName:String,fieldName:String)
trying to run the sample code in the Apress book called Beginning Scala. I
Lift uses a PartialFunction on their implementation of Comet Actors, and you usually end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.