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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:12:54+00:00 2026-06-11T20:12:54+00:00

Starting with some simple code: trait Moveable[A] { def move(a: A): A } trait

  • 0

Starting with some simple code:

trait Moveable[A] {
  def move(a: A): A
}
trait Animal {
  def kick[A <: Animal: Moveable](a: A): A = implicitly[Moveable[A]] move a
}
object Cat {
  implicit object CatMoveable extends Moveable[Cat] {
    def move(cat: Cat): Cat = cat copy (pos = cat.pos + 4)
  }
}
case class Cat(pos: Int) extends Animal
case class Dog(pos: Int) extends Animal
val someAnimal: Animal = Dog(0)
val kickedCat: Cat = someAnimal kick Cat(0)
println(kickedCat) // Cat(4)

I decided to distinguish between, let´s say, Quadruped and Biped animals:

trait FourFeetMoveable[A] {
  def moveWithFourFeets(a: A): A
}
trait TwoFeetMoveable[A] {
  def moveWithTwoFeets(a: A): A
}
trait Animal {
  def kick[A <: Animal /*: ??? */](a: A): A
}
trait Quadruped extends Animal {
  def kick[A <: Animal: FourFeetMoveable](a: A): A = implicitly[FourFeetMoveable[A]] moveWithFourFeets a
}
trait Biped extends Animal {
  def kick[A <: Animal: TwoFeetMoveable](a: A): A = implicitly[TwoFeetMoveable[A]] moveWithTwoFeets a
}
object Chicken {
  implicit object ChickenTwoFeetMoveable extends TwoFeetMoveable[Chicken] {
    def moveWithTwoFeets(chicken: Chicken): Chicken = chicken copy (pos = chicken.pos + 2)
  }
}
case class Dog(pos: Int) extends Quadruped
case class Chicken(pos: Int) extends Biped
val someAnimal: Animal = Dog(0)
val kickedChicken: Chicken = someAnimal kick Chicken(0)
println(kickedChicken) // Chicken(2)

A must have is to have two totally different typeclasses FourFeetMoveable and TwoFeetMoveable, so I can´t abstract over them with something like this:

trait Moveable[A] {
  def move(a: A): A
}

So how can I abstract over the typeclasses used as a context bound at method kick in trait Animal(see the ???)?

EDIT

Sorry, I should have made my example clearer. Lets say the effect of being kicked can be some movement or some other action. I wanted to abstract over that effect with a typeclass.
In the following code I am showing what I mean and also used an abstract type member KickingEffect to abstract over the required typeclass, as 0__ proposed:

trait StumbleEffect[A <: Animal] {
  def stumble(a: A): A
}
trait GlideEffect[A <: Animal] {
  def glide(a: A): A
}
trait Animal {
  type KickingEffect[A <: Animal]
  def kick[A <: Animal: KickingEffect](a: A): A
}
trait Biped extends Animal {
  type KickingEffect[A <: Animal] = StumbleEffect[A]
  override def kick[A <: Animal: StumbleEffect](a: A): A = implicitly[StumbleEffect[A]] stumble a
}
trait Quadruped extends Animal {
  type KickingEffect[A <: Animal] = GlideEffect[A]
  override def kick[A <: Animal: GlideEffect](a: A): A = implicitly[GlideEffect[A]] glide a
}
object Dog {
  implicit object DogGlideEffect extends GlideEffect[Dog] {
    def glide(dog: Dog): Dog = dog copy (pos = dog.pos + 4)
  }
}
case class Dog(pos: Int) extends Quadruped
case class Cat(pos: Int) extends Quadruped
case class Chicken(pos: Int) extends Biped

But then I ran into another problem, when it comes to sequences of animals:

type Beast[A <: Animal, KE[_ <: Animal]] = A { type KickingEffect[X <: Animal] = KE[X] }
val dogBeast: Beast[Dog, GlideEffect] = Dog(0) // fine

type GlideBeasts[A <: Quadruped] = Beast[A, GlideEffect]
val glideBeasts: Seq[GlideBeasts[Quadruped]] = Seq(Dog(0), Cat(0)) // fine

def kickAll[A <: Animal, KE[_ <: Animal], KA <: Animal](kicker: Beast[A, KE])(animals: Seq[KA])(implicit ev: kicker.KickingEffect[KA]): Seq[KA] = {
  for (a <- animals) yield kicker kick a
}
val cat = Cat(0)
val dog = Dog(0)
kickAll(cat)(Seq(dog)) // wrong inferred kinds of type arguments
kickAll[Cat, GlideEffect, Dog](cat)(Seq(dog)) // missing implicit evidence
  • 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-11T20:12:55+00:00Added an answer on June 11, 2026 at 8:12 pm

    Like this?

    trait Moveable[A] {
      def move(a: A): A
    }
    trait FourFeetMoveable[A] extends Moveable[A]
    trait TwoFeetMoveable[A] extends Moveable[A]
    
    trait Animal {
      type CanKick[A] <: Moveable[A]
      def kick[A <: Animal : CanKick](a: A): A = implicitly[CanKick[A]] move a
    }
    trait Quadruped extends Animal {
      type CanKick[A] = FourFeetMoveable[A]
    }
    trait Biped extends Animal {
      type CanKick[A] = TwoFeetMoveable[A]
    }
    

    Regarding your edit: I would recommend at this point not to further try to model that with types, unless it is really an extremely crucial point in your application or a pure thought experiment. You can get easily overambitious with type-safe design, then the ratio between design effort and application value is getting to big; I would just drop some compile-time safety and go for pattern matching and runtime errors.

    If you do want to follow the types route, as soon as you have collections, you will need something like HLists to preserve the individual types of the collection members.


    Anyway, you can make your example work (with explicit type parameters):

    def kickAll[A <: Animal, KE[_ <: Animal], KA <: Animal](
       kicker: Beast[A, KE])(animals: Seq[KA])(implicit effect: KE[KA]): Seq[KA] = {
          for (a <- animals) yield kicker kick a
    }
    
    val cat = Cat(0)
    val dog = Dog(0)
    kickAll(cat)(Seq(dog)) // still doesn't figure out the types
    kickAll[Cat, GlideEffect, Dog](cat)(Seq(dog)) // ok!
    

    As said, the tricky or impossible part comes when you try to do this with heterogeneous lists (e.g. requiring different effects). You might get away with using a helper type class for the elements of the sequence, instead, so that implicits can be resolved per item before.

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

Sidebar

Related Questions

I have some pretty simple Java code in which I'm starting another process and
I wrote some very simple code since I'm just starting C++ and I want
I'm soon going to be starting some iPhone Development (3.0) building a simple app
I am starting to I am learning scala, and trying to build some simple
I am starting to use some Java code which was written by someone else.
I am starting to write some code for A/B testing in a Grails web
So, I'm starting to write some logic for a simple program (toy game on
I've been starting to use Fortran (95) for some numerical code (generating python modules).
I'm a novice/beginner programmer having problems getting some simple client/server C code working. My
I've got some very simple code below that I want to use as a

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.