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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:50:43+00:00 2026-06-15T23:50:43+00:00

Below I have: trait Elem { def compare: Int } case class DiffElem(weight: Int,

  • 0

Below I have:

trait Elem {
  def compare: Int
}

case class DiffElem(weight: Int, len: Int) extends Elem {
  def compare = weight - len;
}

Now there can be many sub-classes of Elem, based on the compare function.
Now I have a function, that reads input from file and generates a list of DiffElem:

def getInput[T <: Elem](): List[T] = {
  var ans: List[T] = List.empty[T]
  for (line <- Source.fromFile("src/week1/jobs.txt").getLines()) {
    val diff = line.split(" ")
    ans = ans match{
      case i:List[DiffElem] =>  new DiffElem(Integer.parseInt(diff(0)), Integer.parseInt(diff(1))) :: ans;
      case _ => ???
    }

  }
  ans
}

But the compiler does not allow the operation, obviously as :: follows contra-variance type and I am trying to do invariance. If I convert the function signature to T >: Elem then it works, but the purpose is not solved.

Any better approach??

  • 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-15T23:50:43+00:00Added an answer on June 15, 2026 at 11:50 pm

    If you’re constructing DiffElem inside getInput, then how (and why) do you want to get list of arbitrary T <: Elem? To do something like this, you should be able to convert from DiffElem to any another T <: Elem, and that convertability could be captured with implicit like this:

      def getInput[T <: Elem](implicit conv: DiffElem => T): List[T] = ...
    

    , but do you really want it? If you’ll be happy with just list of DiffElem’s, you could rewrite your function in functional style this way:

      def getInput(): List[DiffElem] =
        Source.fromFile("src/week1/jobs.txt").getLines().map { line =>
          val diff = line.split(" ")
          DiffElem(
            Integer.parseInt(diff(0)),
            Integer.parseInt(diff(1))
          )
        } toList
    

    map is higher-order function that captures transformation of each element of sequence into the result of application of its funarg to this element.

    Update:
    To make modular type-driven list creation I’ll suggest implicits:

      def getInput[E <: Elem](implicit mkE: (Int, Int) => E): List[E] = {
        Source.fromFile("src/week1/jobs.txt").getLines().map { line =>
          val diff = line.split(" ")      
          mkE(diff(0).toInt, diff(1).toInt)      
        } toList
      }
    

    Now, for any E <: Elem, list of which you want to get, you should provide implicit value of type (Int, Int) => E into the scope, like:

      implicit val diffElemBuilder = {
        (a, b) => DiffElem(a, b)
      }
    

    And in any scope, where this implicit is visible, you could use getInput like this:

    val xs = getInput[DiffElem]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trait T class C extends T compiled to .class files. And the
Below I have an application. Now on the previous page user types in a
I have below structure <div class=mybox> <div id=imageslide> <a href=><img src=url /></a> </div> </div>
I have two of the following java classes (listed below) Class BookInfo declares static
I have the following class, listed below with static field of arrays. Could you
I have a model class defined as below: class Vehicle(models.Model): stock_number = models.CharField(max_length=6) vin
If any of the nested div s below have a length longer then an
Below I have a function that animates a div ( container ) either to
Below I have table - Company id name value year 1 IBM 10 2011
Below I have a line of code where it states which Session a user

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.