Note that there are some explanatory texts on larger screens.

plurals
  1. POError while trying to initialise a class in scala
    primarykey
    data
    text
    <p>I am trying to make a class for the purpose of adding rational numbers</p> <pre><code>class Rational(val numerator: Int, val denominator: Int) { require(denominator != 0) def this(numerator: Int) = this(numerator, 1) def +(addend: Rational): Rational = { new EquivalentRationals(this, addend).sum } def +(addend: Int): Rational = new Rational(numerator + (denominator*addend), denominator) override def toString = numerator + "/" + denominator } class EquivalentRationals(a: Rational, b: Rational) { val first: Rational = new Rational(a.numerator * b.denominator, a.denominator * b.denominator) val second: Rational = new Rational(b.numerator * a.denominator, b.denominator * a.denominator) def sum(): Rational = new Rational(first.numerator + second.numerator, second.denominator) def difference(): Rational = new Rational(first.numerator - second.numerator, second.denominator) override def toString = first + " " + second } </code></pre> <p>I get the following error when i try to create an object out of the first class</p> <blockquote> <p>:23: error: type mismatch; found : Rational(in object $iw)(in object $iw)(in object $iw)(in object $iw) required: Rational(in object $iw)(in object $iw)(in object $iw)(in object $iw) new EquivalentRationals(this,addend).sum()</p> </blockquote> <p>I would like to know why this occurs and i would also be grateful for any suggestions of alternative approaches to adding rational numbers which also use a separate class for representing equivalent fractions. (there is a rational class which already accomplishes this without using a separate class in the book "programming in scala") edit: The problem only seems to occur when using scala-mode in emacs. I tried to run it by selecting the option "load file in interpreter", so although it runs via the command line i am still curious as to why it doesn't in "inferior-scala"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload