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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:18:10+00:00 2026-05-24T20:18:10+00:00

I would like to derive from Scala’s immutable Map. It is defined as such:

  • 0

I would like to derive from Scala’s immutable Map. It is defined as such:

trait Map[A, +B]

Unfortunately, my implementation needs to be invariant in B. I tried the following, but without success:

def +(kv : (A, B)) : MyMap[A, B] = { ... }

override def +[B1 >: B](kv : (A, B1)) : MyMap[A, B1] =
    throw new IllegalArgumentException()

Maybe there is a trick with @uncheckedVariance?

  • 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-24T20:18:11+00:00Added an answer on May 24, 2026 at 8:18 pm

    Getting rid of covariance altogether would of course be unsound, and is not allowed.
    Given m: Map[A, String], and v : Any, you can do val mm : Map[A, Any] = m + v. This is what Map definition says, and all implementors must follow. Your class may be invariant, but it must implement the full covariant interface of Map.

    Now redefining + to throw an error is a different story (not very sound yet). The problem with your new + method is that after generics erasure, it has the same signature than the other + method. There is a trick: add in implicit parameter, so that you have two parameters in the signature, which makes it different from the first one.

    def +(kv : (A,B))(implicit useless: A <:< A) : MyMap[A,B]
    

    (it doesn’t really matter what implicit parameter you’re looking for, as long as one is found. implicit useless: Ordering[String] works just as well)

    Doing that, you have the usual problem with overloading. If you add a B without the compiler knowing it to be so, the failing method will be called. It might be better to perform a type check there so that B instances are accepted whatever. That would require getting a Manifest[B] in your map.

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

Sidebar

Related Questions

I would like to derive a new class from System.Web.UI.WebControls.WebControl , however I think
I have a generic abstract base class from which I would like to derive
I would like to be able to compare two classes derived from the same
Would like to be able to set colors of headings and such, different font
Suppose I have such requirement: The objects in the system all derive from a
I'd like to define a trait with some properties which have a well defined
I would like to assign a drive letter to a GMail drive so that
I have a Show table, and I would like to have a derived type
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it

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.