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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:42:14+00:00 2026-05-26T04:42:14+00:00

Consider the following example, which should print 8. Why does the A.Value + B.Value

  • 0

Consider the following example, which should print 8. Why does the A.Value + B.Value thinks that B.Value should be a string? How do I fix it?

object Catano extends App {
  val const3 = new Constant(3)
  val const5 = new Constant(5)

  val block = new Arithmetic(const3.Result, const5.Result)

  println(block.Sum.Value)
}

class Block

class Arithmetic[T: Numeric](val A: Connector[T], val B: Connector[T]) extends Block {
  def Sum = new Connector({ A.Value + B.Value })
}

class Constant[T](x: T) extends Block {
  def Result = new Connector({ x })
}

class Connector[T](f: => T) {
  def Value: T = f
}

For type-safety reasons, the following should fail with a Type exception:

  val const3 = new Constant("ping")
  val const5 = new Constant("pong")

  val block = new Arithmetic(const3.Result, const5.Result)
  • 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-26T04:42:15+00:00Added an answer on May 26, 2026 at 4:42 am

    Your problem can be reproduced with just:

    class C[T: Numeric] {def add(a: T, b: T) = a+b }
    error: type mismatch;
    found   : T
    required: String
    

    What is happening there : in scala as in java, you can do String + anything, and also anything + String. As, unlike in java, pperators are just normal method calls, that seems to imply there is a corresponding + method on each type. Of course, it is not so, as java types have no such method. What we have is implicit def any2StringAdd(x: Any) in Predef, which makes this + available by implicit conversion. In your code, this is the only + available, which is why it complains B.Value is not a String.

    Now why is the intended + not available? T:Numeric requires that there is a Numeric[T] value in implicit scope. It says nothing about what type T should be, and what methods are available on T. This Numeric[T] instance has a def plus(x: T, y: T): T method. That alone does not make + available on T. You can call plus directly, but this is not covenient. Fortunately, a + delegating to plus can be added by implicit conversion (just like the +(String) was in Predef), provided you put some implicits in scope with :

    import Numeric.Implicits._
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example which should color every second row: ( live demo here
Consider the following simple example class, which has a property that exposes a modified
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider, for example, the following code: class ViewHelpersTest < ActionView::TestCase should 'generate tag with
Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =
Consider the following example. It consists of two header files, declaring two different namespaces:
Consider the following example program: next :: Int -> Int next i | 0
Consider the following example. I have an interface MyInterface, and then two abstract classes
Consider the following example: int size = 10, *kk = new int[size]; for (int

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.