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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:30:18+00:00 2026-05-23T21:30:18+00:00

scala> sealed trait Gender defined trait Gender scala> case object Male extends Gender defined

  • 0
scala> sealed trait Gender
defined trait Gender

scala> case object Male extends Gender
defined module Male

scala> case object Female extends Gender
defined module Female

scala> Map(Male -> Male, Female -> Female, Male -> Female, Female -> Male)
res2: scala.collection.immutable.Map[Product with Gender,Product with Gender] =
        Map((Male,Female), (Female,Male))

Why in the above code, the type of res2 is Map[Product with Gender, Product with Gender] instead of Map[Gender, Gender]? And why of the four entries I supplied to map, only two got added?

  • 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-23T21:30:18+00:00Added an answer on May 23, 2026 at 9:30 pm

    1) Product is the lowest-level superclass of both Male and Female. This is because all case classes extend Product. Both have trait Gender, which Scala recognizes, so it includes that too. This is Scala’s best guess for type inference because it is the most specific type inferable (Map[Gender,Gender] is more general). If you want the type to be Map[Gender,Gender], you can tell it that explicitly:

    scala> val x: Map[Gender,Gender] = Map(Male -> Male, Female -> Female, Male -> Female, Female -> Male)
    x: Map[Gender,Gender] = Map(Male -> Female, Female -> Male)
    

    2) The nature of a Map is that it has only one value for each key. When you add Male -> Male you are mapping Male to Male. When you add Male -> Female you are overwriting the original mapping so that now Male maps to Female. If you want to have all of these mappings, then it’s probably easier to just to a list of pairs since that doesn’t enforce any uniqueness constraints.

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

Sidebar

Related Questions

scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
object Test extends Application { // compiles: Map[Int, Value]( 0 -> KnownType(classOf[Object]), 1 ->
The following is possible in Scala: scala> val l = List l: scala.collection.immutable.List.type =
Learning Scala currently and needed to invert a Map to do some inverted value->key
What is the difference between sealed abstract and abstract Scala class?
I'm writing a ServletUnitTest trait in Scala to provide a convenience API for ServletUnit
It is possible to define sealed classes in Scala, which are basically final except
This is an implementation for a leftist heap in Scala. package my.collections sealed abstract
I have a type like this sealed class Foo[A](val value: A) object Foo {
I have the following scenario: sealed abstract class Type(val inUse: Boolean) case class IntTy(override

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.