Reputation: 1719
I tried to use "spire", a math framework for scala, but I have an unexpected error.Here is my little program:
import spire.algebra._
import spire.implicits._
trait AbGroup[A] extends Group[A]
final class Rationnel_Quadratique(val n1: Int = 2)(val coef: (Int, Int)) {
override def toString = {
coef match {
case (c, i) =>
s"$c + $i√$n"
}
}
def a() = coef._1
def b() = coef._2
def n() = n1
}
object Rationnel_Quadratique {
def apply(coef: (Int, Int),n: Int = 2) {
new Rationnel_Quadratique(n)(coef)
}
}
object AbGroup {
implicit object RQAbGroup extends AbGroup[Rationnel_Quadratique] {
def +(a: Rationnel_Quadratique, b: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique(coef=(a.a() + b.a(), a.b() + b.b())) <---
def inverse(a: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique((-a.a(), -a.b()))
def id: Int = Rationnel_Quadratique((0, 0))
}
}
object euler66_2 extends App {
println("salut")
val a = r"10/7"
val b = r"5/12"
println(a / b)
val c = Rationnel_Quadratique(1, 2)
val d = Rationnel_Quadratique(3, 4)
val e = c + d
println(e)
}
the error relies not on maths but on the apply method : in the line with the arrow the compiler does not allow the use of tuple :
type mismatch; found : Unit required: Rationnel_Quadratique def +(a: Rationnel_Quadratique, b: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique(coef= ^
I would rather not use maps.can you help me?
Upvotes: 1
Views: 99
Reputation: 35463
You have defined the apply
method on the Rationnel_Quadratique
companion to return Unit
. Change the apply
definition to:
def apply(coef: (Int, Int),n: Int = 2) = {
new Rationnel_Quadratique(n)(coef)
}
Adding the =
sign will make sure the apply
method has a return type.
Upvotes: 1