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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:41:24+00:00 2026-05-29T06:41:24+00:00

I’m trying to write some library functions to enhance the basic collections. Most of

  • 0

I’m trying to write some library functions to enhance the basic collections. Most of it has gone smoothly, but I’m having an issue with this one.

class EnhancedGenTraversableLike[A, Repr <: GenTraversable[A]](self: GenTraversableLike[A, Repr]) {
  def mapValuesStrict[T, U, R, That](f: U => R)(implicit ev: A <:< (T, U), bf: CanBuildFrom[Repr, (T, R), That]) = {
    val b = bf(self.asInstanceOf[Repr])
    b.sizeHint(self.size)
    for ((k: T, v: U) <- self) b += k -> f(v)
    b.result
  }
}
implicit def enhanceGenTraversableLike[A, Repr <: GenTraversable[A]](self: GenTraversableLike[A, Repr]) = new EnhancedGenTraversableLike[A, Repr](self)

Here’s what happens when I go to use it:

scala> List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict((_:Int).toString)
res0: List[(Int, java.lang.String)] = List((1,2), (2,3), (3,4), (2,5))

scala> List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict(x => x.toString)
<console>:13: error: missing parameter type
              List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict(x => x.toString)
                                                            ^

So Scala is unable to determine the type of x.

This answer indicates Scala doesn’t use one parameter to resolve another, but that separate parameter lists can fix the problem. In my case, however, this isn’t so easy since the type information is found in the implicit parameters.

Is there a way around this so that I don’t have to specify the type every time I call the method?


Update: Based on Owen’s advice, I ended up creating a enriched class specific to a traversable of pairs:

class EnrichedPairGenTraversableLike[T, U, Repr <: GenTraversable[(T, U)]](self: GenTraversableLike[(T, U), Repr]) {
  def mapValuesStrict[R, That](f: U => R)(implicit bf: CanBuildFrom[Repr, (T, R), That]) = {
    val b = bf(self.asInstanceOf[Repr])
    b.sizeHint(self.size)
    for ((k: T, v: U) <- self) b += k -> f(v)
    b.result
  }
}
implicit def enrichPairGenTraversableLike[T, U, Repr <: GenTraversable[(T, U)]](self: GenTraversableLike[(T, U), Repr]) = new EnrichedPairGenTraversableLike(self)
  • 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-29T06:41:26+00:00Added an answer on May 29, 2026 at 6:41 am

    Yes, there is. Let me give a simpler example. I hope this will also work with
    your more complicated use case.

    say we have

    trait Foo[A]
    
    class Bar {
        def methWithImplicits[A,B](f: A => B)(implicit foo: Foo[A]) = null
    }
    
    implicit def fooInt: Foo[Int] = null
    

    Now this has exactly the problem you describe, since

    (new Bar).methWithImplicits(x => x)
    

    gives “missing parameter type”.

    So what we would like to do, is to move the implicit parameter “behind” the
    explicitly supplied function, so that Scala sees the implicit first. Well,
    one way we can do this is to add an extra layer of indirection:

    class Bar {
        def methWithImplicits2[A](implicit foo: Foo[A]) = new {
            def apply[B](f: A => B) = null
        }
    }
    
    (new Bar).methWithImplicits2.apply(x => x)
    

    This works, though the syntax is not so pretty. One way you might consider
    prettying the syntax is to look at your current design and see if you can sneak
    the implicit into any of the “earlier” stages. For example, since the
    mapValuesStrict method is only meaningful once the implicit has been
    supplied, you might make the implicit a property of the object instead of
    passed to the method.

    But if that is not convenient in your design, you could use an extra implicit
    conversion to sneak it back. This is what we would like to do:

    implicit def addFoo[A](bar: Bar)(implicit foo: Foo[A]) = new {
        def methWithImplicits3[B](f: A => B) = null
    }
    

    But unfortunately there is what I suspect is a bug in Scala that causes it to
    search for an implicit value that is too polymorphic, causing it to complain:

    could not find implicit value for parameter foo: test.Foo[A]
    

    This only happens when using implicit conversions, which is why I think it is a
    bug. So, we can take it back even further: (and, requiring -Xexperimental
    for dependent method types):

    trait FooWrapper {
        type AA
        val foo: Foo[AA]
    }
    
    implicit def wrapFoo[A](implicit theFoo: Foo[A]) = new FooWrapper {
        type AA = A
        val foo = theFoo
    }
    
    implicit def addFoo(bar: Bar)(implicit foo: FooWrapper) = new {
        def methWithImplicits3[B](f: foo.AA => B) = null
    }
    

    And now

    (new Bar).methWithImplicits3(x => x)
    

    works perfectly 😉


    Update

    In your particular case, I think your best bet is to work the implicit into enhanceGenTraversable, though, alas, the same hack is required to work around the possible bug:

    // Notice `ev` is now a field of the class
    class EnhancedGenTraversableLike[A, Repr <: GenTraversable[A], T, U]
        (self: GenTraversableLike[A, Repr], ev: A <:< (T, U))
    {
        def mapValuesStrict[R, That](f: U => R)(implicit bf: CanBuildFrom[Repr, (T, R), That]) = {
            val b = bf(self.asInstanceOf[Repr])
            b.sizeHint(self.size)
            for ((k: T, v: U) <- self) b += k -> f(v)
            b.result
        }
    }
    
    // The Hack
    trait WrappedPairBound[A] {
        type TT
        type UU
        val bound: A <:< (TT, UU)
    }
    
    implicit def wrapPairBound[A,T,U](implicit ev: A <:< (T,U)) = new WrappedPairBound[A] {
        type TT = T
        type UU = U
        val bound = ev
    }
    
    // Take the implicit here
    implicit def enhanceGenTraversableLike[A, Repr <: GenTraversable[A]]
            (self: GenTraversableLike[A, Repr])(implicit ev: WrappedPairBound[A]) =
        new EnhancedGenTraversableLike[A, Repr, ev.TT, ev.UU](self, ev.bound)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.