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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:46:24+00:00 2026-06-17T15:46:24+00:00

In Scala (2.10) if I ask for a List(1.0, 2) then I get a

  • 0

In Scala (2.10) if I ask for a List(1.0, 2) then I get a List[Double], as expected.

But…

if I ask for a Map(1.0 -> 'A', 2 -> 'B') I get a Map[AnyVal, Char]. I’d like the keys to be of type Double. Asking for Map[Double, Char](1.0 -> 'A', 2 -> 'B) gives a type mismatch on ‘2’.

This I find most confusing! Is it not inconsistent?

Aside: List[(Double, Char)]((1.0, 'A'), (2, 'B')).toMap gives me a Map[Double, Char] though.

  • 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-17T15:46:26+00:00Added an answer on June 17, 2026 at 3:46 pm

    If you look into Scala’s type hierarchy, you can see that the primitive types are not in a sub-type/super-type relationship. However, there is a mechanism called numeric widening which for example allows you to call a method that takes a Double argument, by passing in say an Int. The Int then is automatically “widened” to Double.

    This is the reason that List(1.0, 2) gives you List[Double].

    But the Map constructor takes Tuple[A, B] arguments. The numeric widening doesn’t apply to higher order types, so the target type inference doesn’t work for you if you mix numeric types.

    case class Test[A](tup: (A, Char)*)
    Test(1.0 -> 'A', 2 -> 'B') // AnyVal
    

    Moreover, the arrow operator -> gets in your way:

    Test[Double](2 -> 'B') // found: (Int, Char)  required: (Double, Char)
    

    This is another limitation of the type inference I think. Writing a tuple a -> b is syntactic sugar for (a, b), provided by the implicit method any2ArrowAssoc on Predef. Without this indirection, if you construct the Tuple2 directly, it works:

    Test[Double]((2, 'B'))
    

    So the numeric widening still doesn’t work, but at least you can enforce the type:

    Map[Double, Char]((1.0, 'A'), (2, 'B'))
    

    A final example showing numeric widening working:

    def map[A, B](keys: A*)(values: B*) = Map((keys zip values): _*)
    map(1.0, 2)('A', 'B') // Map[Double, Char]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to ask if there is a list of annotations for Scala 2.8.0?
I wanted to ask if there is any type of string interpolation in Scala.
I'd like to write a type alias to shorten, nice and encapsulated Scala code.
I'll ask this with a Scala example, but it may well be that this
I've been programming in Scala for a while and I like it but one
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can
Scala includes the continuations plugin now (yay), but must be enabled by passing -P:continuations:enable
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
In language like python and ruby to ask the language what index-related methods its
I'm embarrassed to ask this, but I cannot figure out how to run a

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.