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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:33:43+00:00 2026-05-20T13:33:43+00:00

Assume you want to add some methods to all Iterables. That can look like

  • 0

Assume you want to add some methods to all Iterables. That can look like this:

import collection.generic.CanBuildFrom

class Foo[P, S[X] <: Iterable[X]](val s : S[P]) {
  def bar(j : P)(implicit bf : CanBuildFrom[S[P],P,S[P]]) : S[P] = {
    val builder = bf(s)
    builder ++= s
    builder += j
    builder.result
  }

  def oneBar(j : P)(implicit bf : CanBuildFrom[S[P],P,S[P]]) : P = bar(j).head
}

implicit def iter2foo[P, S[X] <: Iterable[X]](s : S[P]) = new Foo[P,S](s)

Now, code like

println(Seq(1,2,3,4) bar 5)

compiles and executes smoothly. However,

println((1 to 4) bar 5)

causes

error: value bar is not a member of scala.collection.immutable.Range.Inclusive 
with scala.collection.immutable.Range.ByOne

I figured this might be since the implicit conversion demands (?) that the parameter’s type has a type parameter (which Range has not). But

implicit def iter2foo[P, S <: Iterable[P]](s : S) = new Foo[P,Iterable](s)

does not change anything. Note that Range extends Iterable[Int].

What am I doing wrong? How can I write one implicit conversion that applies to all subtypes of Iterable, wether generic or not?

Edit: I just notices that the much simpler

implicit def iter2foo[P](s : Iterable[P]) = new Foo[P,Iterable](s)

works as intended (on REPL). Or does it? Are there drawbacks of this solution?

Edit 2: The drawback is that the static result type of bar will only be Iterable[P], not the more specific type. The constructed collection has the correct (actual) type, 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-05-20T13:33:44+00:00Added an answer on May 20, 2026 at 1:33 pm

    Sadly, it doesn’t matter that Range extends Iterable[Int], this is indeed a problem with arity of type params. It’s a deep one too, even the core library suffers it in places (just look at the comments in Manifest)

    You’ll also encounter it if wanting to use Maps, Strings, etc. as though they were Iterable.

    The only solution I’ve found is to define multiple implicit conversions to the pimp type.

    UPDATE

    The problem here is in inferring the type parameter P from the supplied argument, which doesn’t appear to have a type parameter. You’re essentially trying to do for a type-constructor what extractors will do for a regular constructor, and polymorphism is getting in the way.

    Your edited example works because this particular inference isn’t needed, the catch is that you can now only return an Iterable, and so lose much of the benefit of CanBuildFrom

    If that’s not a problem, then it’s a simpler solution, so roll with it.

    Otherwise, you’ll need different implicits for each possible arity of the types you want to pimp.

    UPDATE 2

    Consider how the compiler might handle your different expressions when trying to determine if a Range is a valid argument:

    Take 1:

    implicit def iter2foo[P, S[X] <: Iterable[X]](s : S[P]) = new Foo[P,S](s)
    
    • S is a higher-kinded type, of kind * => *
    • Range is a simple type, of kind *
    • The kinds don’t match so it’s invalid

    Take 2:

    implicit def iter2foo[P, S <: Iterable[P]](s : S) = new Foo[P,Iterable](s)
    
    • Same problem, S is still of kind * => * the argument doesn’t match

    Take 3:

    implicit def iter2foo[P](s : Iterable[P]) = new Foo[P,Iterable](s)
    
    • Having had the parameter supplied, Iterable[P] is a simple type of kind *
    • Range passes this first hurdle
    • The second check is that Range is a subclass of Iterable[P] for some P
    • It is, with P inferred as Int
    • The compiler is happy that all inference, bounds checking, etc. has succeeded
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that I want to add a favicon on a dynamically generated page
I want build a sketch pad app on iPhone, I assume that this type
Assume that I have some HTML page, and a corresponding CSS file. I'd like
Assume I want to generate an element similar to this in ASP.NET MVC 2:
Assume that I want to listen to a non-common port (9090 for example) 24/7
Assume that i have created a client TCP Socket in Activity A. I want
I want to add some additional properties to table rows. The table is created
I want to have a gradient in HTML/CSS. Assume some DIV is always more
Assume i have a private routine that performs some calculation: private function TCar.Speed: float
I want to add some data from a NSMutableArray to my UITableView . I

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.