- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers

# How to use generic type in aggregation function

Wed, 2012-01-11, 11:59

Hi scala-user,

I have this aggregation function which will sum all Int input arguments:

**case** **class** Seed(**var** total: Int ) {}

__d__**ef** createSeed = Seed(0)

__d__**ef** sum(seed:Seed, value:Int) = seed.total + value

I also want to sum all Double values, so I have to have a new class:

**case** **class** Seed(**var** total: Double ) {}

__d__**ef** createSeed = Seed(0.0)

__d__**ef** sum(seed:Seed, value:Double) = seed.total + value

Then, I want to make ***Seed** * to be generic so that I can reuse it for all numeric types, e.g. int, double, long

**case** **class** Seed[T:Numeric](**var** total: T ) {

**def** createSeed()(**implicit** num: Numeric[T]) = Seed(num.zero)

**def** sum(seed:Seed[T], value:T) = seed.total + __value__

}

However, it does not work in my scala 2.9.1.

How can I define a generic sum function to calculate any numeric value?

David

Yinwei David Liu **Morgan Stanley | Cross-Technology Services **Kerry Parkside | 1155 Fang Dian Road, Pudong New Area

201204 Shanghai

Phone: +86 21 2035-2552

Yinwei [dot] Liu [at] MorganStanley [dot] com

Wed, 2012-01-11, 13:41

#2
Re: How to use generic type in aggregation function

On Wed, Jan 11, 2012 at 08:59, Liu, Yinwei David

wrote:

> Hi scala-user,

>

>

>

> I have this aggregation function which will sum all Int input arguments:

>

> case class Seed(var total: Int ) {}

>

> def createSeed = Seed(0)

>

> def sum(seed:Seed, value:Int) = seed.total + value

>

>

>

> I also want to sum all Double values, so I have to have a new class:

>

> case class Seed(var total: Double ) {}

>

> def createSeed = Seed(0.0)

>

> def sum(seed:Seed, value:Double) = seed.total + value

>

>

>

> Then, I want to make *Seed * to be generic so that I can reuse it for all

> numeric types, e.g. int, double, long

>

> case class Seed[T:Numeric](var total: T ) {

>

> def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)

>

> def sum(seed:Seed[T], value:T) = seed.total + value

>

> }

>

>

>

> However, it does not work in my scala 2.9.1.

>

>

>

> How can I define a generic sum function to calculate any numeric value?

Well, there's an ambiguous implicit, but, fixing that, it works for me.

scala> import scala.math.Numeric.Implicits._

import scala.math.Numeric.Implicits._

scala> case class Seed[T:Numeric](var total: T ) {

| def createSeed() = Seed(implicitly[Numeric[T]].zero)

| def sum(seed:Seed[T], value:T) = seed.total + value

| }

defined class Seed

Wed, 2012-01-11, 15:31

#3
RE: How to use generic type in aggregation function

Thanks Daniel, adding * scala.math.Numeric.Implicits._* fixed the issue.

David

-----Original Message-----

From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]

Sent: Wednesday, January 11, 2012 8:35 PM

To: Liu, Yinwei David (ISGT)

Cc: scala-user [at] googlegroups [dot] com

Subject: Re: [scala-user] How to use generic type in aggregation function

On Wed, Jan 11, 2012 at 08:59, Liu, Yinwei David

wrote:

> Hi scala-user,

>

>

>

> I have this aggregation function which will sum all Int input arguments:

>

> case class Seed(var total: Int ) {}

>

> def createSeed = Seed(0)

>

> def sum(seed:Seed, value:Int) = seed.total + value

>

>

>

> I also want to sum all Double values, so I have to have a new class:

>

> case class Seed(var total: Double ) {}

>

> def createSeed = Seed(0.0)

>

> def sum(seed:Seed, value:Double) = seed.total + value

>

>

>

> Then, I want to make *Seed * to be generic so that I can reuse it for all

> numeric types, e.g. int, double, long

>

> case class Seed[T:Numeric](var total: T ) {

>

> def createSeed()(implicit num: Numeric[T]) = Seed(num.zero)

>

> def sum(seed:Seed[T], value:T) = seed.total + value

>

> }

>

>

>

> However, it does not work in my scala 2.9.1.

>

>

>

> How can I define a generic sum function to calculate any numeric value?

Well, there's an ambiguous implicit, but, fixing that, it works for me.

scala> import scala.math.Numeric.Implicits._

import scala.math.Numeric.Implicits._

scala> case class Seed[T:Numeric](var total: T ) {

| def createSeed() = Seed(implicitly[Numeric[T]].zero)

| def sum(seed:Seed[T], value:T) = seed.total + value

| }

defined class Seed

On Wed, Jan 11, 2012 at 11:59 AM, Liu, Yinwei David <Yinwei [dot] Liu [at] morganstanley [dot] com> wrote:

scala> def aggregationFunction(intInputArguments: Int*) = intInputArguments.sum

aggregationFunction: (intInputArguments: Int*)Int

scala> aggregationFunction(1, 3, 5)

res5: Int = 9

Like that?

Cheers,

√

--

Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang