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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:35:23+00:00 2026-06-01T07:35:23+00:00

I am having a tough time understanding why the Scala compiler is unhappy about

  • 0

I am having a tough time understanding why the Scala compiler is unhappy about this function definition:

def trimNonWordCharacters[T <: Iterable[String]](items: T): T =
     items map { _.replaceAll("\\W", "") }

Here is the REPL output:

scala> def trimNonWordCharacters[T <: Iterable[String]](items: T): T =
     items map { _.replaceAll("\\W", "") }
<console>:5: error: type mismatch;
 found   : Iterable[java.lang.String]
 required: T
       def trimNonWordCharacters[T <: Iterable[String]](items: T): T = items map { _.replaceAll("\\W", "") }

The goal is to pass in any implementation of an Iterable and get the same type of back out. Is this possible?

  • 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-01T07:35:24+00:00Added an answer on June 1, 2026 at 7:35 am

    The map method on Iterable returns an Iterable, so even if T is a subclass of Iterable, it’s map method will return Iterable.

    To get better typing, you’d have to write it like this:

    import scala.collection.IterableLike
    def trimNonWordCharacters[T <: Iterable[String]](items: T with IterableLike[String, T]): T =
         items map { _.replaceAll("\\W", "") }
    

    However, that won’t work either, because there’s no information that let a map on T to generate another T. For example, mapping a BitSet into a String cannot result in a BitSet. So we need something else: something that teaches how to build a T from a T, where the mapped elements are of type String. Like this:

    import scala.collection.IterableLike
    import scala.collection.generic.CanBuildFrom
    def trimNonWordCharacters[T <: Iterable[String]]
                             (items: T with IterableLike[String, T])
                             (implicit cbf: CanBuildFrom[T, String, T]): T =
         items map { _.replaceAll("\\W", "") }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a tough time understanding the following piece of code: int stride
I'm having a tough time understanding laziness. Can someone help me understand why my
I am having a tough time finding an answer to this seemingly simple question.
I'm having a tough time with this one, I have a class which populates
I'm having a tough time with NetTcpBinding. When I run my WCFservice,I get this:
I'm having a tough time understanding how the two interact and where the boundary
I'm having a tough time formatting this WordPress widget to look good. http://healthybodyguru.com/ It's
I'm having a tough time getting this to work. Maybe my logic is all
I'm having a tough time deciding how to refactor this method in my controller.
I'm having a tough time parsing this date its the +0 at the end

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.