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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:37:18+00:00 2026-05-30T18:37:18+00:00

I’m exploring ways to abstract Case Classes in Scala. For example, here is an

  • 0

I’m exploring ways to abstract Case Classes in Scala. For example, here is an attempt for Either[Int, String] (using Scala 2.10.0-M1 and -Yvirtpatmat):

trait ApplyAndUnApply[T, R] extends Function1[T, R] {
  def unapply(r: R): Option[T]
}

trait Module {
  type EitherIntOrString
  type Left <: EitherIntOrString
  type Right <: EitherIntOrString
  val Left: ApplyAndUnApply[Int, Left]
  val Right: ApplyAndUnApply[String, Right]
}

Given this definition, I could write something like that:

def foo[M <: Module](m: M)(intOrString: m.EitherIntOrString): Unit = {
  intOrString match {
    case m.Left(i) => println("it's an int: "+i)
    case m.Right(s) => println("it's a string: "+s)
  }
}

Here is a first implementation for the module, where the representation for the Either is a String:

object M1 extends Module {
  type EitherIntOrString = String
  type Left = String
  type Right = String
  object Left extends ApplyAndUnApply[Int, Left] {
    def apply(i: Int) = i.toString
    def unapply(l: Left) = try { Some(l.toInt) } catch { case e: NumberFormatException => None }
  }
  object Right extends ApplyAndUnApply[String, Right] {
    def apply(s: String) = s
    def unapply(r: Right) = try { r.toInt; None } catch { case e: NumberFormatException => Some(r) }
  }
}

The unapplys make the Left and Right really exclusive, so the following works as expecting:

scala> foo(M1)("42")
it's an int: 42

scala> foo(M1)("quarante-deux")
it's a string: quarante-deux

So far so good. My second attempt is to use scala.Either[Int, String] as the natural implementation for Module.EitherIntOrString:

object M2 extends Module {
  type EitherIntOrString = Either[Int, String]
  type Left = scala.Left[Int, String]
  type Right = scala.Right[Int, String]
  object Left extends ApplyAndUnApply[Int, Left] {
    def apply(i: Int) = scala.Left(i)
    def unapply(l: Left) = scala.Left.unapply(l)
  }
  object Right extends ApplyAndUnApply[String, Right] {
    def apply(s: String) = scala.Right(s)
    def unapply(r: Right) = scala.Right.unapply(r)
  }
}

But this does not work as expected:

scala> foo(M2)(Left(42))
it's an int: 42

scala> foo(M2)(Right("quarante-deux"))
java.lang.ClassCastException: scala.Right cannot be cast to scala.Left

Is there a way to get the right result?

  • 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-30T18:37:19+00:00Added an answer on May 30, 2026 at 6:37 pm

    The problem is in this matcher:

    intOrString match {
        case m.Left(i) => println("it's an int: "+i)
        case m.Right(s) => println("it's a string: "+s)
    }
    

    It unconditionally executes m.Left.unapply on the intOrString. As to why it does, see below.

    When you call foo(M2)(Right("quarante-deux")) this is what is happening:

    • m.Left.unapply resolves to M2.Left.unapply which is in fact scala.Left.unapply
    • intOrString is Right("quarante-deux")

    Consequently, scala.Left.unapply is called on Right("quarante-deux") which causes CCE.

    Now, why this happens. When I tried to run your code through the interpreter, I got these warnings:

    <console>:21: warning: abstract type m.Left in type pattern m.Left is unchecked since it is eliminated by erasure
               case m.Left(i) => println("it's an int: "+i)
                      ^
    <console>:22: warning: abstract type m.Right in type pattern m.Right is unchecked since it is eliminated by erasure
               case m.Right(s) => println("it's a string: "+s)
                       ^
    

    The unapply method of ApplyAndUnApply gets erased to Option unapply(Object). Since it’s impossible to run something like intOrString instanceof m.Left (because m.Left is erased too), the compiler compiles this match to run all erased unapplys.

    One way to get the right result is below(not sure if it goes along with your original idea of abstracting case classes):

    trait Module {
        type EitherIntOrString
        type Left <: EitherIntOrString
        type Right <: EitherIntOrString
        val L: ApplyAndUnApply[Int, EitherIntOrString]
        val R: ApplyAndUnApply[String, EitherIntOrString]
    }
    
    object M2 extends Module {
        type EitherIntOrString = Either[Int, String]
        type Left = scala.Left[Int, String]
        type Right = scala.Right[Int, String]
        object L extends ApplyAndUnApply[Int, EitherIntOrString] {
            def apply(i: Int) = Left(i)
            def unapply(l: EitherIntOrString) = if (l.isLeft) Left.unapply(l.asInstanceOf[Left]) else None
        }
        object R extends ApplyAndUnApply[String, EitherIntOrString] {
            def apply(s: String) = Right(s)
            def unapply(r: EitherIntOrString) = if (r.isRight) Right.unapply(r.asInstanceOf[Right]) else None
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of 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.