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 8502931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:37:16+00:00 2026-06-11T01:37:16+00:00

Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting

  • 0

Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting parameter a, and a disturbingly ubiquitous parameter b that would be nice to hide away, when extracting c.

[EDIT]: It appears something was broken in my intellij/scala-plugin installation that caused this. I cannot explain. I was having numerous strange problems with my intellij lately. After reinstalling, I can no longer reprodce my problem. Confirmed that unapply/unapplySeq do allow for implicit parameters! Thanks for your help.

This does not work (**EDIT:yes, it does):**

trait A; trait C; trait B { def getC(a: A): C }

def unapply(a:A)(implicit b:B):Option[C] = Option(b.getC(a))

In my understanding of what an ideal extractor should be like, in which the intention is intuitively clear also to Java folks, this limitation basically forbids extractor objects which depend on additional parameter(s).

How do you typically handle this limitation?

So far I’ve got those four possible solutions:

1) The simplest solution that I want to improve on: don’t hide b, provide parameter b along with a, as normal parameter of unapply in form of a tuple:

object A1{ 
    def unapply(a:(A,B)):Option[C] = Option(a._2.getC(a._1)) }

in client code:

 val c1 = (a,b) match { case A1(c) => c1 }

I don’t like it because there is more noise deviating that deconstruction of a into c is important here. Also since java folks, that have to be convinced to actually use this scala code, are confronted with one additional synthactic novelty (the tuple braces). They might get anti-scala aggressions “What’s all this? … Why then not use a normal method in the first place and check with if?”.

2) define extractors within a class encapsulating the dependence on a particular B, import extractors of that instance. At import site a bit unusual for java folks, but at pattern match site b is hidden nicely and it is intuitively evident what happens. My favorite. Some disadvantage I missed?

class BDependent(b:B){ 
   object A2{ 
    def unapply(a:A):Option[C] = Option(b.getC(a))
         } }

usage in client code:

val bDeps = new BDependent(someB)
import bDeps.A2 
val a:A = ...
val c2 = a match { case A2(c) => c }
}

3) declare extractor objects in scope of client code. b is hidden, since it can use a “b” in local scope. Hampers code reuse, heavily pollutes client code (additionally, it has to be stated before code using it).

4) have unapply return Option of function B => C. This allows import and usage of an ubitious-parameter-dependent extractor, without providing b directly to the extractor, but instead to the result when used. Java folks maybe confused by usage of function values, b not hidden:

 object A4{
  def unapply[A,C](a:A):Option[B => C] = Option((_:B).getC(a))
   }

then in client code:

 val b:B = ...
 val soonAC: B => C = a match { case A4(x) => x }
 val d = soonAC(b).getD ...

Further remarks:

  • As suggested in this answer, “view bounds” may help to get extractors work with implicit conversions, but this doesn’t help with implicit parameters. For some reason I prefer not to workaround with implicit conversions.
  • looked into “context bounds”, but they seem to have the same limitation, don’t they?
  • 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-06-11T01:37:18+00:00Added an answer on June 11, 2026 at 1:37 am

    In what sense does your first line of code not work? There’s certainly no arbitrary prohibition on implicit parameter lists for extractor methods.

    Consider the following setup (I’m using plain old classes instead of case classes to show that there’s no extra magic happening here):

    class A(val i: Int)
    class C(val x: String)
    class B(pre: String) { def getC(a: A) = new C(pre + a.i.toString) }
    

    Now we define an implicit B value and create an extractor object with your unapply method:

    implicit val b = new B("prefix: ")
    
    object D {
      def unapply(a: A)(implicit b: B): Option[C] = Option(b getC a)
    }
    

    Which we can use like this:

    scala> val D(c) = new A(42)
    c: C = C@52394fb3
    
    scala> c.x
    res0: String = prefix: 42
    

    Exactly as we’d expect. I don’t see why you need a workaround here.

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

Sidebar

Related Questions

Apparently visual studio 2010 built DLLs do not support Windows 2000. Is there a
Apparently mutableCopy copies by reference, not value. Ie if I do this: NSMutableArray arrayA
Apparently, there's been a big brouhaha over whether or not Python needs tail-call optimization
Apparently (at least according to gcc -std=c99 ) C99 doesn't support function overloading. The
Apparently I've done something I'm not aware of All my classes now have warnings
Apparently, I'm not understanding how to use the ContinueWith method. My goal is to
Apparently this function in SDL_Mixer keeps dying, and I'm not sure why. Does anyone
Apparently I don't understand something about binding ListBox controls to data. Here is a
Apparently this is a fairly often experienced issue. I'm not sure entirely where the
Apparently I'm missing something obvious here, but would appreciate a quick example. I've got

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.