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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:47:06+00:00 2026-06-16T11:47:06+00:00

I am using Guava’s EventBus in my Scala project. I have a parameterized event

  • 0

I am using Guava’s EventBus in my Scala project.

I have a parameterized event like so:

class MyEvent[T]

And a simple event listener:

class MyEventListener {

    @Subscribe
    def onStringEvent(event: MyEvent[String]) {
        println("String event caught")
    }

    @Subscribe
    def onIntEvent(event: MyEvent[Int]) {
        println("Int event caught")
    }

}

I can create my com.google.common.eventbus.EventBus, register MyEventListener, and fire an event:

val eventBus = new EventBus
eventBus.register(new MyEventListener)
eventBus.post(new MyEvent[String])

But, as you may have guessed already, both onStringEvent and onIntEvent get called as a result. The issue is that Java’s/Scala’s type erasure drops off the parameter type at runtime and both subscriptions appear to Guava as event: MyEvent.

Ok, my question:

Due to erasure, using the same Event object for different types of Guava events in this manner wouldn’t be possible in Java and isn’t possible in Scala. However, Scala proves to have a number of nice ways to circumvent Java’s erasure problems. Does anybody see another way to achieve this, perhaps using some Scala wizardry?

  • 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-16T11:47:08+00:00Added an answer on June 16, 2026 at 11:47 am

    The problem is in Guava: it cannot see the type parameter, and so it will not distinguish between the two methods. The only possible solution is to create a new class for each type.

    That can be really easy:

    class MyEvent[T] protected () { /* Your methods here */ }
    class MyEventInt extends MyEvent[Int] {}
    class MyEventString extends MyEvent[String] {}
    

    and then whenever you need to do anything in your code, just use MyEvent[Int]. But Guava will require at least this much boilerplate.

    Note that I’ve made the MyEvent[T] constructor protected so you have to instantiate one of the de-generified classes. I’m not sure whether that will work for your use-case; I’ll assume so. You can get around that also (with type classes), but it adds more boilerplate.

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

Sidebar

Related Questions

In Java, I like to start methods with using Guava's Preconditions class to make
Using Guava, I would like to have a map that has the following properties:
I have a project which would like to utilize the Google Guava libraries (on
I'm using google guava 12 and have a map: Map<OccupancyType, BigDecimal> roomPrice; I have
I'm using MultiMap from Google Guava 12 like this: Multimap<Integer, OccupancyType> pkgPOP = HashMultimap.create();
Can I easily convert InputStream to BufferedReader using Guava? I'm looking for something like:
I am using Guava to create an EventBus for a publish-subscribe messaging service. I
I am considering using the Optional< T > class in Guava library to handle
Background I have been using the Authorize.net SDK in an Eclipse project of it's
I have been using guava for some time now and truly trusted it, until

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.