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

  • Home
  • SEARCH
  • 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 8126399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:08:04+00:00 2026-06-06T07:08:04+00:00

Lets I have a utility class called MathUtil. and it looks like this .

  • 0

Lets I have a utility class called MathUtil.

and it looks like this .

abstract class MathUtil(T:Numeric){
   def nextNumber(value:T)
   def result():T
}

Lets I subclass it this way

class SumUtil[T:Numeric] extends MathUtil[T]{
   private var sum:T = 0
   override def nextNumber(value:T){
     sum = sum + value
   }
   override def result():T = sum
}

I have a problem with the statement

private var sum:T = 0

Now , I have to initialize to sum to 0. I would guess any numeric to have a way to represent 0. Im pretty new to scala. How do I solve this issue ?

  • 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-06T07:08:06+00:00Added an answer on June 6, 2026 at 7:08 am

    The Numeric type class instance has a zero method that does what you want:

    class SumUtil[T: Numeric] extends MathUtil[T] {
       private var sum: T = implicitly[Numeric[T]].zero
       override def nextNumber(value: T) {
         sum = implicitly[Numeric[T]].plus(sum, value)
       }
       override def result(): T = sum
    }
    

    Note that you also need the instance for the plus method, unless you import Numeric.Implicits._, in which case you can use +. You can also clean the code up a bit by not using the context bound syntax in this case:

    class SumUtil[T](implicit ev: Numeric[T]) extends MathUtil[T] {
       import Numeric.Implicits._
       private var sum: T = ev.zero
       override def nextNumber(value: T) {
         sum = sum + value
       }
       override def result(): T = sum
    }
    

    This is exactly equivalent: the context bound version is just syntactic sugar for this implicit argument, but if you need to use that argument explicitly (as you do here, for its zero), I find it cleaner to write the desugared version.

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

Sidebar

Related Questions

Lets say I have a few utility functions in file tests/utils/functions.js. I would like
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say I have hierarchy like this (This is just a test program. Please
I encountered a class during my work that looks like this: public class MyObject
Lets say that I have a list of objects like so: public class FlatModel
Let's say I have a utility class DateUtil (see below). To use this method
Lets have this situation (in c++, in c# classes A,B are interfaces): class A
I have lets say a WeeklyViewUserControl.xaml and a DailyViewUserControl.xaml. Normally I used stuff like
Lets say I have model inheritance set up in the way defined below. class

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.