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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:15:03+00:00 2026-06-11T10:15:03+00:00

Say I have an implicit conversion: implicit def aToB(a: A):B={ … } How can

  • 0

Say I have an implicit conversion:

implicit def aToB(a: A):B={
...
}

How can I get this implicit conversion to work on the elements of a List?

If I have:

val listOfA: List[A] ...

and I have a function that takes a List of B, is it possible to let Scala implicitly convert all of the elements from A’s to B’s?

Without implicit conversions, the conversion might look like:

lisftOfA.map(a => new B(a.someValue, a.anotherValue))

But I would love for this to happen like ‘magic’… is that too much to ask.

  • 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-11T10:15:04+00:00Added an answer on June 11, 2026 at 10:15 am

    Here are a few alternatives you might wish to consider:

    1. Use a view bound

    If it’s possible to change the function that takes a List of Bs, this would be the simplest solution. Modify it to accept a List of things that can be converted to Bs. That is,

    def yourFn(l: List[B]) = ...
    

    would become

    def yourFn[X <% B](l: List[X]) = ...
    

    Then, you can just call the function with listOfA:

    yourFn(listOfA)
    

    2. Introduce a conversion method

    This is similar to Rogach’s first solution, except that the outer conversion is non-implicit:

    def convert[B, A <% B](l: List[A]): List[B] = l map { a => a: B }
    

    Then at your function’s call-site, you would write

    yourFn(convert(listOfA))
    

    Like Rogach’s second solution, this is bit safer than bringing in an implicit conversion.

    3. Introduce an implicit conversion

    This is equivalent to Rogach’s first solution, but the notation is a bit nicer (IMO).

    implicit def convert[B, A <% B](l: List[A]): List[B] = l map { a => a: B }
    

    If this conversion is in scope at your call-site, you can just call your function with the listOfA:

    yourFn(listOfA)
    

    Parting Thoughts

    It’s interesting to consider how to solve this problem in a general way. What if I want to define my conversion method so that it can handle any type that implements the map method? i.e.,

    def convert[B, A <% B, C[_]](c: C[A]): C[B] = c map { a => a: B }
    

    This won’t work, of course, since nothing in the signature expresses the constraint that C must implement map. As far as I know, expressing this constraint is fairly involved and cannot be done in a way that provides out-of-the-box support for any type that implements map. See Type-safe Scala sequence comprehensions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a function with a signature like this: def tsadd(key: Any,
Say I have a simple object which supports implicit casting to System.String public sealed
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have this: float i = 1.5 in binary, this float is
Let's say we have this class: public class Moo { string _value; public Moo(string
This really should be easy, but I just can't work it out myself, the
I have started reading JSP. I came across JSP implicit object, say for example,
Say I have a method that turns a (function on two elements) into a
This is minor, I know, but let's say that I have a class Character
Say have normal class in iOS, called A. I'd like to pass this 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.