beginner
beginner

Reputation: 468

Trouble referencing packages in IntelliJ Scala

I'm working through Martin Odersky's MOOC on Functional Programming in Scala. In his Lecture 3.2, he shows how you can refer to a class stored in a package using the Eclipse IDE. However, I am working with the IntelliJ IDE and I have trouble reproducing his results. I've tried looking around SO and Google but the questions are about advanced packaging related issues.

In short, he has a package called week3, and inside it he has 2 files. The first is the Rational scala class file with contents:

package week3

class Rational(x: Int, y: Int) {
  require(y != 0, "denominator must be nonzero")
  def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
  val g = gcd(x, y)
  def numer = x / g
  def denom = y / g

  def this(x: Int) = this(x, 1)
  def + (that: Rational): Rational = {
    new Rational(
      numer * that.denom + denom * that.numer,
      denom * that.denom)
  }

  def unary_- : Rational = new Rational(-numer, denom)
  def - (that: Rational) = this + -that
  def < (that: Rational) = this.numer * that.numer < this.denom * that.numer
  def max(that: Rational) = if (this < that) that else this
  override def toString = {
    numer+ "/" + denom
  }
}

And the second file is the scratch scala file with the code below. He obtains the result in the following picture.

object scratch {
  new week3.Rational(1,2)
}

Results

My attempt: I've tried reproducing the same results in IntelliJ by creating a 'week3' package within my src folder and including the code for Rational in a class file in the week3 package. Next I created a Scala worksheet in the src folder with the code:

new week3.Rational(1,2)

The IDE doesn't flag any errors, but nothing is shown on the interactive output on the right. Also when I try evaluating the worksheet it returns an error: Error ... not found

Can I get some help setting me on the right track please?

Upvotes: 0

Views: 89

Answers (1)

Samar
Samar

Reputation: 2101

Check the "Make Project" checkbox next to the "Interactive Mode" checkbox. That should fix this issue. I had faced this irritating problem myself :)

enter image description here

Upvotes: 1

Related Questions