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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:09:49+00:00 2026-05-12T21:09:49+00:00

Following on from another question I asked, Scala 2.8 breakout , I wanted to

  • 0

Following on from another question I asked, Scala 2.8 breakout, I wanted to understand a bit more about the Scala method TraversableLike[A].map whose signature is as follows:

def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That

Notice a few things about this method:

  • It takes a function turning each A in the traversable into a B.
  • It returns That and takes an implicit argument of type CanBuildFrom[Repr, B, That].

I can call this as follows:

> val s: Set[Int] = List("Paris", "London").map(_.length)
s: Set[Int] Set(5,6)

What I cannot quite grasp is how the fact that That is bound to B (that is, it is some collection of B’s) is being enforced by the compiler. The type parameters look to be independent of both the signature above and of the signature of the trait CanBuildFrom itself:

trait CanBuildFrom[-From, -Elem, +To]

How is the Scala compiler ensuring that That cannot be forced into something which does not make sense?

> val s: Set[String] = List("Paris", "London").map(_.length) //will not compile

How does the compiler decide what implicit CanBuildFrom objects are in scope for the call?

  • 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-12T21:09:49+00:00Added an answer on May 12, 2026 at 9:09 pm

    Note that the second argument to map is an implicit argument. There must be an implicit in scope with the appropriate types, or, otherwise, you must pass such an argument.

    In your example, That must be Set[String], B must be Int and Repr must be List[String]. Therefore, for that to compile you need the following implicit object in scope:

    implicit object X: CanBuildFrom[List[String], Int, Set[String]]
    

    There’s no such thing in scope. Also, breakOut can’t provide it, because it, itself, needs an implicit CanBuildFrom, whose first type can be any class (a contra-variant descendant of Nothing), but otherwise restricted by the other types.

    Take a look, for instance, on the CanBuildFrom factory from the companion object of List:

    implicit def  canBuildFrom [A] : CanBuildFrom[List, A, List[A]]  
    

    Because it binds the second and third parameters through A, the implicit in question won’t work.

    So, how does one know where to look for, regarding such implicits? First of all, Scala does import a few things into all scopes. Right now, I can recall the following imports:

    import scala.package._ // Package object
    import scala.Predef._  // Object
    // import scala.LowPriorityImplicits, class inherited by Predef
    import scala.runtime._ // Package
    

    Since we are concerned about implicits, note that when you import things from packages, the only implicits possible are singletons. When you import things from objects (singletons), on the other hand, you can have implicit definitions, values and singletons.

    Right now, there are CanBuildFrom implicits inside Predef and LowPriorityImplicits, which are concerned with strings. They enable us to write "this is a string" map (_.toInt).

    So, barring these automatic imports, and the explicit imports you make, where else can an implicit be found? One place: the companion objects of the instance on which the method is being applied.

    I say companion objects, in the plural, because the companion objects of all traits and classes inherited by the class of the instance in question may contain relevant implicits. I’m not sure if the instance itself may contain an implicit. To be honest, I can’t reproduce this right now, so I’m certainly making a mistake of some kind here.

    At any rate, look inside the companion objects.

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

Sidebar

Related Questions

Following on from a question I posted yesterday about GUIs, I have another problem
Following on from another question I have asked I have created a new class
I've got the following code from another question on SO to track the changing
I found the following regex from another Stack Overflow question: Change an element's class
In another question, a user posted the following reflog: 8c48bab HEAD@{16}: checkout: moving from
OK, so this is leading on from another question I asked here recently. Basically,
I have the following css from another question: .title {color:#707070;} .username {color:#8DAAB8;} .dateandtime {color:#A5A7AC;}
Today i was asked this question about sharing data from a thread t1 that
Following on from another question I had here I've got a MainConfig object and
The following code is from a details page that receives a querystring from another

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.