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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:28:30+00:00 2026-05-27T14:28:30+00:00

If I define the following generic event handler trait Handles[E <: Event] { def

  • 0

If I define the following generic event handler

trait Handles[E <: Event] {
  def handle(event: E)
}

with event type’s like this

trait Event {

}
class InventoryItemDeactivated(val id: UUID) extends Event;

class InventoryItemCreated(val id: UUID, val name: String) extends Event;

how do I then make a single class that implements event handlers for each of these events ?. I tried:

class InventoryListView extends Handles[InventoryItemCreated] with Handles[InventoryItemDeactivated] {
    def handle(event: InventoryItemCreated) = {

    }

    def handle(event: InventoryItemDeactivated) = {

    }
  }

but Scala complains that a trait cannot be inherited twice.

I found this answer which hints at a solution, but it seams to require multiple classes (one for each handler). Is this really the only way or is there then some other Scala construct I can use to make a single class implement multiple generic event handlers (i.e. using case classes, manifests or some other fancy construct) ?.

  • 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-27T14:28:31+00:00Added an answer on May 27, 2026 at 2:28 pm

    I don’t know of a way to do it in one class (except by making Event an ADT and defining handle to accept a parameter of type Event. But that would take away the kind of typesafety you seem to be looking for).

    I’d suggest using type-class pattern instead.

    trait Handles[-A, -E <: Event] {
      def handle(a: A, event: E)
    }
    
    trait Event {
      ...
    }
    class InventoryItemDeactivation(val id: UUID) extends Event
    class InventoryItemCreation(val id: UUID, val name: String) extends Event
    
    class InventoryListView {
      ...
    }
    
    implicit object InventoryListViewHandlesItemCreation extends 
        Handles[InventoryListView, InventoryItemCreation] = {
      def handle(v: InventoryListView, e: InventoryItemCreation) = {
        ...
      }
    }
    
    implicit object InventoryListViewHandlesItemDeactivation extends 
        Handles[InventoryListView, InventoryItemDeactivation] = {
      def handle(v: InventoryListView, e: InventoryItemDeactivation) = {
        ...
      }
    }
    
    def someMethod[A, E <: Event](a: A, e: E)
                  (implicit ev: InventoryListView Handles InventoryItemCreation) = {
      ev.handle(a, e)
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following console application example, the event is defined like this: public delegate
I'm trying to define a generic class that takes a parameterized type T and
I wish to define the following typeclass Mapping : {-# LANGUAGE MultiParamTypeClasses #-} class
I have a class FourByFourBoard that extends GameBoard . I define the following fields:
I defined a custom routed event with the following constructs (names changed): public class
I'm currently working on a project in which I'd like to define a generic
I have the following code public abstract class Event { public void fire(Object... args)
In C# we can define a generic type that imposes constraints on the types
I'm trying to define a generic addition operator for a wrapper class. So far
Given a generic class definition like public class ConstrainedNumber<T> : IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>,

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.