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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:42:31+00:00 2026-05-31T02:42:31+00:00

Here is a reduced example of what I want to do. The line commented

  • 0

Here is a reduced example of what I want to do. The line commented out doesn’t compile.

class Animal
object Animal { implicit def toElephant(a: Animal) = a.asInstanceOf[Elephant] }
class Elephant extends Animal

object Main {

    def main(args: Array[String]) = {
        val a: List[Animal] = List(new Elephant, new Elephant)
        // val e: List[Elephant] = a
    }
}

In a regular situation, e = a is of course illegal. But with the implicit function, one could think that scala would automatically convert every element in the list.

Is there an elegant way to get this behaviour? If yes, how?

What I wish to know is if there is some obscure corner of scala that can force the behaviour I wish. I am not interested in solutions that add cruft. I can think of them myself. For instance, one could do:

object Animal {
    implicit def toElephant(a: Animal) = a.asInstanceOf[Elephant]
    implicit def toElephant(a: List[Animal]) = a.asInstanceOf[List[Elephant]] }

and the code above would work.

  • 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-31T02:42:32+00:00Added an answer on May 31, 2026 at 2:42 am

    You can forget implicits and mapping and use simply

    val e = a.asInstanceOf[List[Elephant]]
    

    BUT, if you insist, you can of course also do this conversion implicitly. Having an implicit from List[A] to List[B] is no more crufty than an implicit A => B. I wouldn’t do this, but if I did I’d generalise it for re-use, importing locally to limit the implicit’s scope:

    object Implicits {
      implicit def AListToBList[A, B <: A](lst: List[A]) = lst.asInstanceOf[List[B]]
    }
    

    Then at use-site

    import Implicits._
    val a: List[Animal] = List(new Elephant, new Elephant)
    val e: List[Elephant] = a
    e.head.doSomethingElephanty
    

    Be aware of the scope of the import, and stick things within a locally block if necessary.

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

Sidebar

Related Questions

So here's a contrived example of what I want to have improved or confirmed.
To reduce dependece of class, I want to send parameter (using generic class) to
I have a class which has a well-defined responsibility - to Enrich an object
I have a class that uses either a static list (firstFriend in the example)
INSERT INTO contacts_lists (contact_id, list_id) SELECT contact_id, 67544 FROM plain_contacts Here I want to
I have a custom class with a serialize method, and I want to be
i want to create some simple wrapper classes for an existing class library. To
I have a specific situation where I've got an object that I want to
I am using Entity Framework 4.1 code first . Here is my Category class:
I have a User class and want to display many users' thumbnails w/ their

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.