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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:57:29+00:00 2026-06-15T23:57:29+00:00

I am implementing a GUI event system in Scala. I have something like: case

  • 0

I am implementing a GUI event system in Scala. I have something like:

case class EventObject
case class KeyEventObject extends EventObject
case class MouseEventObject extends EventObject

I would like to store event listener closures in a (multi-)map, like so:

var eventListeners = new MultiMap[EventDescriptor, (EventObject => Unit)];

My question is, is there some way to rewrite this so that the function signatures of the stored closure can be EventObject or any subclass? Something like the following:

var eventListeners = new MultiMap[EventDescriptor, [A <: EventObject](A => Unit)]

so that I can have the subtype known when I define the listener functions:

eventListeners.put(KEY_EVENT, (e:KeyEventObject) => { ... })
eventListeners.put(MOUSE_EVENT, (e:MouseEventObject) => { ... })
  • 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-15T23:57:29+00:00Added an answer on June 15, 2026 at 11:57 pm

    Not that many things are impossible. You can do the following, for example, with type classes:

    class HMultiMap {
      import scala.collection.mutable.{ Buffer, HashMap }
    
      type Mapping[K, V]
    
      private[this] val underlying = new HashMap[Any, Buffer[Any]]
    
      def apply[K, V](key: K)(implicit ev: Mapping[K, V]) =
        underlying.getOrElse(key, Buffer.empty).toList.asInstanceOf[List[V]]
    
      def add[K, V](key: K)(v: V)(implicit ev: Mapping[K, V]) = {
        underlying.getOrElseUpdate(key, Buffer.empty) += v
        this
      }
    }
    

    And now:

    sealed trait EventObject
    case class KeyEventObject(c: Char) extends EventObject
    case class MouseEventObject(x: Int, y: Int) extends EventObject
    
    sealed trait EventDescriptor
    case object KEY_EVENT extends EventDescriptor
    case object MOUSE_EVENT extends EventDescriptor
    
    class EventMap extends HMultiMap {
      class Mapping[K, V]
    
      object Mapping {
        implicit object k extends Mapping[KEY_EVENT.type, KeyEventObject => Unit]
        implicit object m extends Mapping[MOUSE_EVENT.type, MouseEventObject => Unit]
      }
    }
    

    It’s a little messy, but the usage is much prettier:

    val eventListeners = new EventMap
    
    eventListeners.add(KEY_EVENT)((e: KeyEventObject) => println(e.c))
    eventListeners.add(MOUSE_EVENT)((e: MouseEventObject) => println("X: " + e.x))
    eventListeners.add(KEY_EVENT)((e: KeyEventObject) => println(e.c + " again"))
    

    We can confirm that we can pick out individual kinds of event handlers:

    scala> eventListeners(KEY_EVENT).size
    res3: Int = 2
    

    And we can pretend to fire an event and run all the handlers for it:

    scala> eventListeners(KEY_EVENT).foreach(_(KeyEventObject('a')))
    a
    a again
    

    And it’s all perfectly safe, since nothing gets into the underlying loosely-typed map without the proper evidence. We’d get a compile-time error if we tried to add a function from String to Unit, for example.

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

Sidebar

Related Questions

My Goal I would like to have a main processing thread (non GUI), and
I have a GUI with a flat style for the buttons. I would like
Here is the problem I'm running into - I have a GUI class implementing
So I'm implementing a svg-editor-like GUI on one application I'm working here. These are
I would like to know why the BIOS is single-threaded even we have 4
I'm running some background threads in the GUI. Currently I'm implementing a personal Thread
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I'm designing GUI (graphical user interface) system for a game engine (C++). Idea is
I am working with a QT GUI. I am implementing a simple hex edit
I am implementing a GUI (swing) Application comprising three bundles, let say bundleA, bundleB,

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.