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

  • SEARCH
  • Home
  • 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 6727373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:02:31+00:00 2026-05-26T10:02:31+00:00

Consider the following piece of code: case class User(id: Int, name: String) object User{

  • 0

Consider the following piece of code:

case class User(id: Int, name: String)
object User{
  def unapply(str: String) = Some(User(0, str))
}

Scala complains “error: cannot resolve overloaded unapply; case class User(id: Int, str: String)”
Is it not possible to overload unapply?

update:
unapply with larger tuple size.

case class User(id: Int, str: String)
object User{
  def unapply(s: String) = Some((User(0, s), s, 1234))
}

compiler still complains “cannot resolve overloaded unapply”

  • 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-26T10:02:32+00:00Added an answer on May 26, 2026 at 10:02 am

    Your unapply method could not be used in pattern matching

    It works with

    def unapply(arg: <type to match>) : Option[(<matched fields types>)]
    

    (no tuple if only one field, boolean instead than Option if no field).

    The standard unapply of User would be (The Scala Language Specification p. 67)

    def unapply(u: User) = 
      if (u eq null) None 
      else Some((u.id, u.name))
    

    It what you want is to match a user with a zero id as in

    user match {case User(name) => ....}
    

    that would be

    def unapply(u: User): Option[String] = 
      if(u eq null || u.id != 0) None 
      else Some(u.name)
    

    If you want that a string can match as a User (this would be rather bizarre)

    def unapply(s: String): Option[User] = Some(User(0, s))
    

    It would work with

    "john" match case User(u) => ... // u is User(0, john)
    

    I guess you want the former one. In which case both your apply and the standard one are two methods with the same argument list (one User parameter), so they are not compatible. This may be seen as a little unfortunate, as when methods are called as extractors, the distinguishing element is actually the size of the result tuple, not the type of the arguments.

    Your method however, while not valid as an extractor, causes no conflict. I could not find something in the spec that would forbid it. Still, it is useless and a useful method would rightly not be allowed.

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

Sidebar

Related Questions

Consider the following piece of Java code. int N = 10; Object obj[] =
Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == foo) {
Consider the following piece of code: class B { private: // some data members
Consider the following lines of code: public interface IProduct { string Name { get;
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Consider the following piece of Haskell code: type Parser a = String -> [(a,
Consider the following code piece: ... int N,var; vector<int> nums; cin >> N; while
Consider the following piece of code: class foo { private function m() { echo
Consider the following piece of LaTeX code: \begin{tabular}{p{1in}p{1in}} A & B\\ C & D\\
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)

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.