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

The Archive Base Latest Questions

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

I am trying to rewrite some java math classes into Scala, but am having

  • 0

I am trying to rewrite some java math classes into Scala, but am having an odd problem.

class Polynomials[@specialized T](val coefficients:List[T]) {
  def +(operand:Polynomials[T]):Polynomials[T] = {
    return new Polynomials[T](coefficients = 
      (operand.coefficients, this.coefficients).zipped.map(_ + _))
  }
}

My problem may be similar to this question: How do I make a class generic for all Numeric Types?, but when I remove the @specialized I get the same error.

type mismatch; found : T required: String

The second underscore in the map function is highlighted for the error, but I don’t think that is the problem.

What I want to do is have:

Polynomial(1, 2, 3) + Polynomial(2, 3, 4) return Polynomial(3, 5, 7)

And Polynomial(1, 2, 3, 5) + Polynomial(2, 3, 4) return Polynomial(3, 5, 7, 5)

For the second one I may have to pad the shorter list with zero elements in order to get this to work, but that is my goal on this function.

So, how can I get this function to compile, so I can test it?

  • 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-15T08:03:09+00:00Added an answer on June 15, 2026 at 8:03 am

    List is not specialized, so there’s not much point making the class specialized. Only Array is specialized.

    class Poly[T](val coef: List[T]) {
      def +(op: Poly[T])(implicit adder: (T,T) => T) = 
        new Poly(Poly.combine(coef, op.coef, adder))
    }
    object Poly {
      def combine[A](a: List[A], b: List[A], f: (A,A) => A, part: List[A] = Nil): List[A] = {
        a match {
          case Nil => if (b.isEmpty) part.reverse else combine(b,a,f,part)
          case x :: xs => b match {
            case Nil => part.reverse ::: a
            case y :: ys => combine(xs, ys, f, f(x,y) :: part)
          }
        }
      }
    }
    

    Now we can

    implicit val stringAdd = (s: String, t: String) => (s+t)
    
    
    scala> val p = new Poly(List("red","blue"))
    p: Poly[String] = Poly@555214b9
    
    scala>     val q = new Poly(List("fish","cat","dog"))
    q: Poly[String] = Poly@20f5498f
    
    scala>     val r = p+q; r.coef
    r: Poly[String] = Poly@180f471e
    res0: List[String] = List(redfish, bluecat, dog)
    

    You could also ask the class provide the adder rather than the + method, or you could subclass Function2 so that you don’t pollute things with implicit addition functions.

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

Sidebar

Related Questions

I'm trying to rewrite some old SQL into LINQ to SQL. I have a
I'm trying to rewrite some javascript code into Python. Which brings some problems since
I am trying to rewrite some server php code which logs into a website,
I am trying to rewrite links with .htaccess file. Basic stuff, but for some
I'm trying to traverse a simple XML document with Java, but for some reason
Scala/Android newbie question. I'm trying to rewrite this simple code from Java to Scala.
I'm trying to rewrite some URLs, but am not sure how to do what
I'm trying to rewrite some query strings I have in my URL like this
I am trying to get mod_rewrite to work with my site but for some
I'm trying to convert some simple .htaccess rewrites into web.config xml; all is well

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.