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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:02:26+00:00 2026-05-23T20:02:26+00:00

Could someone give a quick explanation why implicit conversion doesn’t work in these cases?

  • 0

Could someone give a quick explanation why implicit conversion doesn’t work in these cases? Thanks.

scala> implicit def strTwoInt (s: String):Int = s.toCharArray.map{_.asDigit}.sum
strTwoInt: (s: String)Int

scala> List[Int]("1","2","3") sum
res3: Int = 6

scala> List("1","2","3") sum
<console>:9: error: could not find implicit value for parameter num: Numeric[java.lang.String]
        List("1","2","3") sum

scala> val a = List("1","2","3")                          
scala> a.foldLeft(0)((i:Int, j:Int) => i+j)
<console>:10: error: type mismatch;
 found   : (Int, Int) => Int
 required: (Int, java.lang.String) => Int
  • 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-23T20:02:28+00:00Added an answer on May 23, 2026 at 8:02 pm

    Your implicit conversion converts a String in an Int. In your first example, it is triggered by the fact that you try to put Strings into a List of Ints.

    In your second example, you have a List of String and you call the method sum, which takes an implicit Numeric[String]. Your conversion does not apply because you neither try to pass a String somewhere the compiler was expecting an Int, nor you tried to call a method which is defined in Int and not in String. In that case, you can either define a Numeric[String] which use explicitly your conversion, or use a method which takes a List[Int] as parameter (giving hint to the compiler):

    scala> def sumIt( xs: List[Int] ) = xs sum
    sumIt: (xs: List[Int])Int
    
    scala> sumIt( List("1","2","3") )
    res5: Int = 6
    

    In the third example, the foldLeft second argument must be of type:

    (Int,String) => Int
    

    the one you actually passed is of type:

    (Int,Int) => Int
    

    however, you did not define any implicit conversion between these two types. But:

    a.foldLeft(0)((i:Int, j:String) => i+j)
    

    triggers your conversion and works.

    Edit: Here’s how to implement the Numeric[String]:

    implicit object StringNumeric extends math.Numeric[String] {
      val num = math.Numeric.IntIsIntegral
      def plus(x: String, y: String) = num.plus(x,y).toString
      def minus(x: String, y: String) = num.minus(x,y).toString
      def times(x: String, y: String) = num.times(x,y).toString
      def negate(x: String): String = num.negate(x).toString
      def fromInt(x: Int) = x.toString
      def toInt(x: String) = x
      def toLong(x: String) = toInt(x)
      def toFloat(x: String) = toInt(x)
      def toDouble(x: String) = toInt(x)
      def compare(x:String,y:String) = num.compare(x,y)
    }
    
    scala> List("1","2","3") sum
    res1: java.lang.String = 6
    

    It works, but the result is a String.

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

Sidebar

Related Questions

Could someone give me an explanation for these two switches? Are they mutually exclusive?
Could someone give me a quick overview of the pros and cons of using
Could someone give me an explanation of the 3rd & 6th params of the
Wonder if someone could give me a quick hand. I have 2 select queries
I find this really strange..could someone give an explanation? abstract class UIController{ public static
Could someone give some info regarding the different character sets within visual studio's project
Could someone give me a little direction on creating crossfades/dissolves between two UIImageViews in
Without spending a long time reviewing the boost source code, could someone give me
Could someone please give me a link on how to create a query in
I was hoping someone could give me a hand on how I write this

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.