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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:48:00+00:00 2026-05-26T14:48:00+00:00

I have a map where both the keys and values are generic types. Something

  • 0

I have a map where both the keys and values are generic types. Something like this:

Map[Foo[A], Bar[A]]

What I’d like to express is that the type A may be different for each key-value pair in the map, but every key is always parameterized with the same type as the value that it maps to. So a Foo[Int] always maps to a Bar[Int], a Foo[String] always maps to a Bar[String], and so forth.

Does anyone know a way to express this?

EDIT:

Here’s an example of the sort of thing I’m trying to do:

trait Parameter // not important what it actually does

class Example {
  val handlers: Map[_ <: Parameter, (_ <: Parameter) => _] = Map()

  def doSomething() {
    for ((value, handler) <- handlers) {
      handler(value)
    }
  }
}

The idea is that a value will always map to a function that can accept it as a parameter, but as the code is written now, the compiler can’t know this.

  • 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-26T14:48:01+00:00Added an answer on May 26, 2026 at 2:48 pm

    As it turns out, it is possible to define a heterogeneous map in Scala. Here’s a rough sketch:

    class HMap[A[_], B[_]] extends Iterable[HMap.Mapping[A, B, _]] {
      private val self = mutable.Map[A[_], B[_]]()
    
      def toMapping[T](a: A[_], b: B[_]): HMap.Mapping[A, B, T] = {
        HMap.Mapping(a.asInstanceOf[A[T]], b.asInstanceOf[B[T]])
      }
    
      def iterator: Iterator[HMap.Mapping[A, B, _]] =
        new Iterator[HMap.Mapping[A, B, _]] {
          val sub = self.iterator
    
          def hasNext = sub.hasNext
          def next(): HMap.Mapping[A, B, _] = {
            val (key, value) = sub.next()
            toMapping(key, value)
          }
        }
    
      def update[T](key: A[T], value: B[T]) = (self(key) = value)
      def get[T](key: A[T]) = self.get(key).asInstanceOf[Option[B[T]]]
      def apply[T](key: A[T]) = self(key).asInstanceOf[B[T]]
    }
    
    object HMap {
      case class Mapping[A[_], B[_], T](val key: A[T], val value: B[T])
    }
    

    This could be made completely typesafe by internally using a linked list of mappings instead of a map, but this is better for performance.

    My original example would look like this:

    object Example {
      type Identity[T] = T
      type Handler[T] = (T) => _
    
      val handlers = new HMap[Identity, Handler]
    
      def doSomething() {
        for (HMap.Mapping(value, handler) <- handlers) {
          handler(value)
        }
      }
    }
    

    This is almost perfect, except I’m not sure how to add bounds.

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

Sidebar

Related Questions

I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
Imagine you have a map like this: (def person { :name { :first-name John
I have a map which contains integer values. i want to re-arange this map
I have a map for my game, I have a script that on a
I have a map of a building and I'd like to programmatically highlight specific
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have a map that represents a DB object. I want to get 'well
This method that uses method-level generics, that parses the values from a custom POJO,
In my Map application I have segment controller on main screen and using that
I have a domain object used in an identity map (the keys are the

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.