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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:32:59+00:00 2026-06-13T16:32:59+00:00

I want to define a type-class like this: trait CanFold[-T, R] { def sum(acc:

  • 0

I want to define a type-class like this:

trait CanFold[-T, R] {
  def sum(acc: R, elem: T): R
  def zero: R
}

implicit object CanFoldInts extends CanFold[Int, Int] {
  def sum(x: Int, y: Int) = x + y
  def zero = 0
}

implicit object CanFoldSeqs extends CanFold[Traversable[_], Traversable[_]] {
  def sum(x: Traversable[_], y: Traversable[_]) = x ++ y
  def zero = Traversable()
}

def sum[A, B](list: Traversable[A])(implicit adder: CanFold[A, B]): B = 
  list.foldLeft(adder.zero)((acc,e) => adder.sum(acc, e))

However, the problem is when I do this I get a Traversable[Any] and it
would be nice to get a Traversable[Int] instead:

 scala> sum(List(1,2,3) :: List(4, 5) :: Nil)
 res10: Traversable[Any] = List(1, 2, 3, 4, 5)

To make matters worse, I cannot define an implicit for
Traversable[Int] after defining one for Traversable[_], because then
the definitions would cause ambiguity. After pulling my hair out I
gave up.

Is there any way I could make that sum return a
Traversable[T] instead of a Traversable[Any]?

Looking at how sum() is defined on Seq in Scala’s library, I can see it works with Numeric, which is invariant, but I want default implementations for supertypes and having the result be different than the input (same as the fold operation) is nice.

  • 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-13T16:33:01+00:00Added an answer on June 13, 2026 at 4:33 pm

    The only way I know to add type parameters to such type classes is to use a def instead of an object:

    implicit def CanFoldSeqs[A] = new CanFold[Traversable[A], Traversable[A]] {
      def sum(x: Traversable[A], y: Traversable[A]) = x ++ y
      def zero = Traversable()
    }
    
    scala> sum(List(1, 2, 3) :: List(4, 5) :: Nil)
    res0: Traversable[Int] = List(1, 2, 3, 4, 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a ref type variable, like this: SomeType v=null; and this
So I have a FileReader class, it looks like this: #define DISALLOW_COPY(type) \ type(const
I want something like this: class Foo<T>{...} class Boo<T>{ Queue<T> stuff = new Queue<T>();
I want to provide something like this in my api: class Foobar extends AbstractThing<Double>
I want to do something like this: sealed abstract class Base(val myparam:String) case class
I'd like to define a class type inside my namespace, but I can't think
I want to define a type name in a templated class that I can
We have some code that looks like this: class Serializer { public: template<class Type>
I want to define that the first element will be of a specific type,
I want to create my own custom collection type. I define my collection as:

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.