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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:11:28+00:00 2026-05-28T02:11:28+00:00

Is it possible to perform a pattern match whose result conforms to a type

  • 0

Is it possible to perform a pattern match whose result conforms to a type parameter of the outer method? E.g. given:

trait Key[A] {
  def id: Int
  def unapply(k: Key[_]): Boolean = k.id == id // used for Fail2
  def apply(thunk: => A): A = thunk // used for Fail3
}

trait Ev[A] {
  def pull[A1 <: A](key: Key[A1]): Option[A1]
}

trait Test extends Ev[AnyRef] {
  val key1 = new Key[String] { def id = 1 }
  val key2 = new Key[Symbol] { def id = 2 }
}

Is there an implementation of Test (its pull method) which uses a pattern match on the key argument and returns Option[A1] for each key checked, without the use of asInstanceOf?

Some pathetic tries:

class Fails1 extends Test {
  def pull[A1 <: AnyRef](key: Key[A1]): Option[A1] = key match {
    case `key1` => Some("hallo")
    case `key2` => Some('welt)
  }
}

class Fails2 extends Test {
  def pull[A1 <: AnyRef](key: Key[A1]): Option[A1] = key match {
    case key1() => Some("hallo")
    case key2() => Some('welt)
  }
}

class Fails3 extends Test {
  def pull[A1 <: AnyRef](key: Key[A1]): Option[A1] = key match {
    case k @ key1() => Some(k("hallo"))
    case k @ key2() => Some(k('welt))
  }
}

None works, obviously… The only solution is to cast:

class Ugly extends Test {
  def pull[A1 <: AnyRef](key: Key[A1]): Option[A1] = key match {
    case `key1` => Some("hallo".asInstanceOf[A1])
    case `key2` => Some('welt  .asInstanceOf[A1])
  }
}

val u = new Ugly
u.pull(u.key1)
u.pull(u.key2)
  • 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-28T02:11:28+00:00Added an answer on May 28, 2026 at 2:11 am

    The problem is indeed that pattern matching ignores all erased types. However, there is a little implicit trickery that one could employ. The following will preserve the type resolution provided by the match for the return type.

    abstract class UnErased[A]
    implicit case object UnErasedString extends UnErased[String]
    implicit case object UnErasedSymbol extends UnErased[Symbol]
    
    class UnErasedTest extends Test {
      def pull[ A1 <: AnyRef ]( key: Key[ A1 ])(implicit unErased: UnErased[A1]): Option[ A1 ] = unErased match {
        case UnErasedString if key1.id == key.id => Some( "hallo" )
        case UnErasedSymbol if key2.id == key.id => Some( 'welt )
        case _ => None
      }
    }
    
    val u = new UnErasedTest 
    println( u.pull( u.key1 ) )
    println( u.pull( u.key2 ) )
    

    This is however nearly equivalent to just defining separate sub classes of Key. I find the following method preferable however it may not work if existing code is using Key[String] that you can’t change to the necessary KeyString (or too much work to change).

    trait KeyString extends Key[String]
    trait KeySymbol extends Key[Symbol]
    
    trait Test extends Ev[ AnyRef ] {
       val key1 = new KeyString { def id = 1 }
       val key2 = new KeySymbol { def id = 2 }
    }
    
    class SubTest extends Test {
      def pull[ A1 <: AnyRef ]( key: Key[ A1 ]): Option[ A1 ] = key match {
        case k: KeyString if key1.id == k.id => Some( "hallo" )
        case k: KeySymbol if key2.id == k.id => Some( 'welt )
        case _ => None
      }
    }
    
    val s = new SubTest
    println( s.pull( s.key1 ) )
    println( s.pull( s.key2 ) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to perform a named-group match in Perl's regex syntax as with
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it even possible to perform address (physical, not e-mail) validation? It seems like
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform an action when a UITableViewCell is highlighted? For example
In SQL Server is it possible to perform table partition without having to recode
What are possible reasons why NHibernate does not perform delete operation? public bool Delete(MyType
can i perform select single node from IXMLDOMElement ,if its possible give me one
Is it possible to use a jQuery ajax call to perform Forms Authentication with
Is it possible for the nose unit testing framework to perform tests during the

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.