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

  • Home
  • SEARCH
  • 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 8752719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:17:20+00:00 2026-06-13T13:17:20+00:00

This is cross-posted from the coursera functional programming course because there’s a lot less

  • 0

This is cross-posted from the coursera functional programming course because there’s a lot less activity on that forum.

I wrote the following code (parts are redacted because it’s homework):

type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
  val mx: Map[Char, Int] = x toMap
  y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}

This produces the following compile error:

type mismatch; found : Map[Char,Int] required: <:<[(Char, Int), (?, ?)]

However if I add a copy of the third line, without the toList, in between via a val statement, the error goes away:

type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
  val mx: Map[Char, Int] = x toMap
  val foo: Map[Char, Int] = y.foldLeft (redacted) (redacted => simple expression using updated and -))
  y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}

I’m guessing this has something to do with giving some kind of extra hint to the type checker, but does anyone know specifically why this happens?

  • 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-13T13:17:21+00:00Added an answer on June 13, 2026 at 1:17 pm

    Below follows a few examples and some explanations on why it happens.

    First, a working and a non-working cases:

    scala> { List('a -> 1, 'b -> 2).toMap
         | println("aaa") }
    aaa
    
    scala> { List('a -> 1, 'b -> 2) toMap
         | println("aaa") }
    <console>:9: error: type mismatch;
     found   : Unit
     required: <:<[(Symbol, Int),(?, ?)]
                  println("aaa") }
                         ^
    

    This happens because the syntax “obj method arg” is considered to be “obj.method(arg)” and so is “obj method \n arg”, this way the argument can be written in the next line. Notice below:

    scala> { val x = List('a -> 1, 'b -> 2) map 
         | identity
         | 
         | println(x) }
    List(('a,1), ('b,2))
    

    It’s the same as List('a -> 1, 'b -> 2).map(identity).

    Now for the weird error message found : Unit, required: <:<[(Symbol, Int),(?, ?)]. It happens that toMap actually takes one argument, here is it’s signature:

    def toMap[T, U](implicit ev: <:<[A,(T, U)]): Map[T,U],

    but it’s an implicit argument, so doesn’t need to be provided explicitly in this case. But when you use the obj method \n arg syntax it fills the method argument. In the above non-working example the argument is println which has type Unit, hence it is not accepted by the compiler.

    One workaround is to have two \n to separate the lines:

    scala> { List('a -> 1, 'b -> 2) toMap
         | 
         | println("aaa") }
    aaa
    

    You can also use a ; to separate the lines.

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

Sidebar

Related Questions

Note: I am cross-posting this from App Engine group because I got no answers
This is cross-posted on the ggplot2 google group My situation is that I'm working
I just came a cross this nice code that makes this scatter matrix plot:
I'm cross-posting this from the wordpress section, b/c I think the problem lies more
This is already cross-posted at MS Connect: https://connect.microsoft.com/VisualStudio/feedback/details/560451 I am attempting to override the
Cross-posted from the Cukes Google Group: I have experimented with a number of methods
EDIT: To fix mistake in covariance matrix Cross posted from here Ok so I
Note: I've cross-posted this question in the grails-user mailing list This weekend using this
First, I'll admit this is cross-posted at SuperUser but I decided to also post
(Note: I originally posted this on drupal.org before remembering that I never get 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.