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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:02:35+00:00 2026-06-11T07:02:35+00:00

I borrowed the MyType trick from Landei here . But recently I ran into

  • 0

I borrowed the MyType trick from Landei here. But recently I ran into a problem with the self type. An example shows what I mean:

trait Excitable[SELF] { self: SELF =>
  def withMoreAnger: SELF
}
trait Animal[SELF0] { self: SELF0 =>
  type SELF = SELF0 // to reveal SELF0 for method spitAt used as a dependent method type 
  type SpittableAnimal[S] <: Animal[S]
  def spitAt[A <: SpittableAnimal[_]](a: A): a.SELF
}
trait ExcitableAnimal[SELF] extends Animal[SELF] with Excitable[SELF] { self: SELF =>
  type SpittableAnimal[S] = ExcitableAnimal[S]
  def spitAt[A <: SpittableAnimal[_]](a: A): a.SELF = a.withMoreAnger
}
trait Quadruped[SELF] extends ExcitableAnimal[SELF] { self: SELF => }
case class Dog(anger: Int) extends Quadruped[Dog] {
  def withMoreAnger: Dog = copy(anger = anger + 1)
}
case class Cat(anger: Int) extends Quadruped[Cat] {
  def withMoreAnger: Cat = copy(anger = anger + 1)
}
val dog = Dog(anger = 0)
val cat = Cat(anger = 0)
val angryCat: Cat = dog spitAt cat // fine

val anotherDog = Dog(0)
val animals = Seq(dog, cat)
val angryAnimals: Seq[Quadruped[_]] = for (a <- animals) yield anotherDog spitAt a // fine
val veryAngryAnimals: Seq[Quadruped[_]] = for (a <- angryAnimals) yield anotherDog spitAt a // type mismatch !!!

As far as I can reveal it, the problem seems to be that the underscore in the spitAt method yields an Any for a.SELF in the end. But how can I make this code work?
I also tried this:

def spitAt[A <: SpittableAnimal[A]](a: A): A = a.withMoreAnger

But then inferred type arguments do not conform to method spitAt’s type parameter bounds which is clear to me, since the SELF type argument of the elements in animals are at least bounded by _ >: Cat with Dog <: Quadruped[_] which do not conform with a.SELF, A in the spitAt above or even A in the spitAt below:

def spitAt[A <: SpittableAnimal[S], S <: A](a: A): A = a.withMoreAnger

So what is the right signature of the spitAt method to make the for-loop lines work?
Perhaps variance annotations (+SELF) of the SELF type parameter may be helpful, but I don´t know how.

  • 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-11T07:02:37+00:00Added an answer on June 11, 2026 at 7:02 am

    Meanwhile I read on and remembered this one: The Typeclass Pattern – An Alternative to Inheritance
    And as user1296806 mentioned here, typeclasses are worth a try. So here it is:

    trait Excitable[T] { // TYPECLASS
      def withMoreAnger(t: T): T
    }
    trait Animal {
      type SpittableAnimal <: Animal
      def spitAt[A <: SpittableAnimal: Excitable](a: A): A
    }
    trait ExcitableAnimal extends Animal {
      type SpittableAnimal = ExcitableAnimal
      def spitAt[A <: SpittableAnimal: Excitable](a: A): A = implicitly[Excitable[A]] withMoreAnger a
    }
    
    object Dog {
      implicit object ExcitableDog extends Excitable[Dog] {
        def withMoreAnger(dog: Dog): Dog = dog copy (anger = dog.anger + 1)
      }
    }
    case class Dog(anger: Int) extends Quadruped
    
    object Cat {
      implicit object ExcitableCat extends Excitable[Cat] {
        def withMoreAnger(cat: Cat): Cat = cat copy (anger = cat.anger + 1)
      }
    }
    case class Cat(anger: Int) extends Quadruped
    
    sealed trait Quadruped extends ExcitableAnimal // sealed: to couple pattern match at implicit object ExcitableQuadruped and all subclasses of Quadruped
    object Quadruped {
      implicit object ExcitableQuadruped extends Excitable[Quadruped] {
        def withMoreAnger(quadruped: Quadruped): Quadruped = {
          quadruped match {
            case dog: Dog => implicitly[Excitable[Dog]].withMoreAnger(dog)
            case cat: Cat => implicitly[Excitable[Cat]].withMoreAnger(cat)
          }
        }
      }
    }
    
    val dog = Dog(anger = 0)
    val cat = Cat(anger = 0)
    val angryCat: Cat = dog spitAt cat // fine
    val anotherDog = Dog(0)
    val animals: Seq[Quadruped] = Seq(dog, cat)
    val angryAnimals: Seq[Quadruped] = for (a <- animals) yield anotherDog spitAt a // fine
    val podAnimals: Seq[Quadruped] = for (a <- angryAnimals) yield anotherDog spitAt a // fine, still a Seq[Quadruped]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This following code example is borrowed from MSDN here . I am not getting
Here is a piece of code borrowed from the Endogine engine. It is supposed
I borrowed the code from http://jqueryui.com/demos/autocomplete/#combobox but don't know how to get the value
I 'borrowed' a regex from this website : http://daringfireball.net/2010/07/improved_regex_for_matching_urls that is almost complete but
I borrowed some code from a site, but I don't know how to get
I borrowed the following method from somewhere on the internet (Can't remember where). But
I'm working with some code I borrowed from here which uses boost::call_once to make
I have a class that I borrowed from here that loads and image from
This is borrowed from another question I made on the site, but would have
I borrowed a piece of code from somewhere, but I don't understand it. It

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.