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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:20+00:00 2026-05-26T23:43:20+00:00

I’m trying to abstract case classes in a module using dependent method types and

  • 0

I’m trying to abstract case classes in a module using dependent method types and a nightly build of the compiler (2.10.0.r26005-b20111114020239). I found some inspiration from Miles Sabin’ example.

I don’t really understand what’s wrong in the (self-contained) code below. The output depends on the order of the patterns in foo.

// afaik, the compiler doesn't not expose the unapply method
// for a companion object
trait Isomorphic[A, B] {
  def apply(x: A): B
  def unapply(x: B): Option[A]
}

// abstract module
trait Module {
  // 3 types with some contraints
  type X
  type Y <: X
  type Z <: X
  // and their "companion" objects
  def X: Isomorphic[Int, X]
  def Y: Isomorphic[X, Y]
  def Z: Isomorphic[Y, Z]
}

// an implementation relying on case classes
object ConcreteModule extends Module {
  sealed trait X { val i: Int = 42 }
  object X extends Isomorphic[Int, X] {
    def apply(_s: Int): X = new X { }
    def unapply(x: X): Option[Int] = Some(x.i)
  }
  case class Y(x: X) extends X
  // I guess the compiler could do that for me
  object Y extends Isomorphic[X, Y]
  case class Z(y: Y) extends X
  object Z extends Isomorphic[Y, Z]
}

object Main {
  def foo(t: Module)(x: t.X): Unit = {
    import t._
    // the output depends on the order of the first 3 lines
    // I'm not sure what's happening here...
    x match {
      // unchecked since it is eliminated by erasure
      case Y(_y) => println("y "+_y)
      // unchecked since it is eliminated by erasure
      case Z(_z) => println("z "+_z)
      // this one is fine
      case X(_x) => println("x "+_x)
      case xyz => println("xyz "+xyz)
    }
  }
  def bar(t: Module): Unit = {
    import t._
    val x: X = X(42)
    val y: Y = Y(x)
    val z: Z = Z(y)
    foo(t)(x)
    foo(t)(y)
    foo(t)(z)
  }
  def main(args: Array[String]) = {
    // call bar with the concrete module
    bar(ConcreteModule)
  }
}

Any idea?

  • 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-26T23:43:21+00:00Added an answer on May 26, 2026 at 11:43 pm

    The warnings are correct and to be expected because, as viewed from within foo, Y and Z will both have been erased to their bounds, ie. X.

    What’s more surprising is that the presence of either the match against Y or the match against Z frustrate the match against X, ie. in this case,

    def foo(t: Module)(x: t.X): Unit = {
      import t._
      // the output depends on the order of the first 3 lines
      // I'm not sure what's happening here...
      x match {
        // unchecked since it is eliminated by erasure
        // case Y(_y) => println("y "+_y)
        // unchecked since it is eliminated by erasure
        // case Z(_z) => println("z "+_z)
        // this one is fine
        case X(_x) => println("x "+_x)
        case xyz => println("xyz "+xyz)
      }
    }
    

    the result is,

    x 42
    x 42
    x 42
    

    which seems reasonable, whereas with one of the earlier matches restored,

    def foo(t: Module)(x: t.X): Unit = {
      import t._
      // the output depends on the order of the first 3 lines
      // I'm not sure what's happening here...
      x match {
        // unchecked since it is eliminated by erasure
        case Y(_y) => println("y "+_y)
        // unchecked since it is eliminated by erasure
        // case Z(_z) => println("z "+_z)
        // this one is fine
        case X(_x) => println("x "+_x)
        case xyz => println("xyz "+xyz)
      }
    }
    

    the result is,

    xyz AbstractMatch$ConcreteModule$X$$anon$1@3b58fa97
    y AbstractMatch$ConcreteModule$X$$anon$1@3b58fa97
    xyz Z(Y(AbstractMatch$ConcreteModule$X$$anon$1@3b58fa97))
    

    which doesn’t: I can’t see any good reason why the additional case would cause xyz to be chosen over X, so I think you’ve run into a bug in the pattern matcher. I suggest you search the Scala JIRA for similar issues, and if you can’t find one, open a ticket with a minimized reproducing example extracted from the above.

    To be honest in the second example above, I would have expected the Y case to have been chosen in all three instances thanks to Y being erased to X and the Y case preceeding the X case in the match expression. But we’re in unchecked territory here and I’m not 100% confident of my intuitions.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We're building an app, our first using Rails 3, and we're having to build
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker

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.