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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:12:30+00:00 2026-06-17T05:12:30+00:00

The trait TraversableLike[+A, +Repr] allows one to make a collection where some functions will

  • 0

The trait TraversableLike[+A, +Repr] allows one to make a collection where some functions will return a Repr, while others continue to return the type parameter That on the function. Is there a way to define a CustomCollection[A] where functions like map, ++, and others will default That as Repr if not inferred otherwise?

Here is a code snippet that hopefully describes what I would like:

case class CustomCollection[A](list: List[A]) extends TraversableLike[A, CustomCollection[A]] {
  protected[this] def newBuilder = new CustomCollectionBuilder[A]
  def foreach[U](f: (A) => U) {list foreach f}
  def seq = list
}

class CustomCollectionBuilder[A] extends mutable.Builder[A, CustomCollection[A]] {
  private val list = new mutable.ListBuffer[A]()
  def += (elem: A): this.type = {
    list += elem
    this
  }
  def clear() {list.clear()}
  def result(): CustomCollection[A] = CustomCollection(list.result())
}

object CustomCollection extends App {
  val customCollection = CustomCollection(List(1, 2, 3))
  println(customCollection filter {x => x == 1}) // CustomCollection(1)
  println(customCollection map {x => x + 1}) // non-empty iterator
}

I would like the last line to be CustomCollection(2, 3, 4).

  • 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-17T05:12:31+00:00Added an answer on June 17, 2026 at 5:12 am

    You need to set up a companion object which provides the refined CanBuildFrom instance:

    import collection.TraversableLike
    import collection.generic.{CanBuildFrom, GenericCompanion, GenericTraversableTemplate,
      TraversableFactory}
    import collection.mutable.{Builder, ListBuffer}
    
    object CustomCollection extends TraversableFactory[CustomCollection] {
      def newBuilder[A] = new CustomCollectionBuilder[A]
      implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, CustomCollection[A]] = 
        new CanBuildFrom[Coll, A, CustomCollection[A]] {
           def apply(): Builder[A, CustomCollection[A]] = new CustomCollectionBuilder()
           def apply(from: Coll): Builder[A, CustomCollection[A]] = apply()
        }
    }
    case class CustomCollection[A](list: List[A]) extends Traversable[A]
    with TraversableLike[A, CustomCollection[A]]
    with GenericTraversableTemplate[A, CustomCollection] {
      override def companion: GenericCompanion[CustomCollection] = CustomCollection
      def foreach[U](f: A => U) { list foreach f }
      override def seq = list
    }
    
    class CustomCollectionBuilder[A] extends Builder[A, CustomCollection[A]] {
      private val list = new ListBuffer[A]()
      def += (elem: A): this.type = {
        list += elem
        this
      }
      def clear() {list.clear()}
      def result(): CustomCollection[A] = CustomCollection(list.result())
    }
    
    val customCollection = CustomCollection(List(1, 2, 3))
    val f = customCollection filter {x => x == 1} // CustomCollection[Int]
    val m = customCollection map {x => x + 1}     // CustomCollection[Int]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a Trait that some classes use but not others, how can
Direct type recursion just works: trait TT[T<:TT[T]] But I can made indirect one with
Say I have a number of trait s that each add some behaviour to
I have the following definitions: trait Xode[+R] { type S = Option[R] } case
HI Could anyone give a sample program to implement the is_same_type type trait in
trait B { type MyInnerType def foo: MyInnerType } object B1 extends B {
How would I convert: trait Foo[A <: Foo[A]] to a type member? I.e., I
I remember seeing a trait that will automatically add the created and updated dates
Observe the following code trait Example { type O def apply(o: O) def f(o:
When implementing a Scala trait, one has to implement the trait's methods and the

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.