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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:00:36+00:00 2026-05-24T09:00:36+00:00

In my use case I have a class with covariant type Foo[+T] and classes

  • 0

In my use case I have a class with covariant type Foo[+T] and classes A <: T, B <: T, C <: T,
I need to store a map “A” -> a Foo[A] instance, “B” -> a Foo[B] instance and “C” -> a Foo[C] instance, is this possible in scala?

Currently I declare my map as Map[String, Foo[T]]] but then I can’t add a Foo[A] inside, i’m being told by the compiler that Foo[T] is expected, not Foo[A], which seems caused by the lack of covariance of the Map parameter, is there a solution?

My workaround for now is to de-parameterize Foo and cast in the code, which of course doesn’t please me, I saw also that I could use java collections instead but I would prefer staying with scala.

Thanks in advance for your time

  • 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-24T09:00:36+00:00Added an answer on May 24, 2026 at 9:00 am

    Works for me, even with non-covariant mutable Maps.

    Welcome to Scala version 2.9.1.RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
    
    scala> class T
    defined class T
    
    scala> class A extends T
    defined class A
    
    scala> class Foo[+T]
    defined class Foo
    
    scala>   val m = collection.mutable.Map[Int, Foo[T]]()
    m: scala.collection.mutable.Map[Int,Foo[T]] = Map()
    
    scala>   val m2 = m + (1 -> new Foo[A])
    m2: scala.collection.mutable.Map[Int,Foo[T]] = Map(1 -> Foo@48f2818d)
    
    scala>   m += (2 -> new Foo[A])
    res0: m.type = Map(2 -> Foo@12caa79c)
    

    If Foo[T] was not covariant in T, then the last command (m += (2 -> new Foo[A])) would fail.

    Note that this code works because Tuples are also covariant in their types.

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

Sidebar

Related Questions

I have a use case where I only need to store certain fields to
I have the following use case: There's a class called Template and with that
I have following use-case: there are several assemblies decorated with ProtoContract classes and I
I have a use case where I need to call a (non-static) method in
I have this use case of an xml file with input like Input: <abc
I have a use case with NServiceBus explained in this question. Essentially there is
I have this use case that is very similar to the robot-legs example of
For example, I have this case class: case class Student (firstName : String, lastName
I have a use case where I need to return a PDF to a
Here's the use case: I have this long form with group of field that

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.