1

I am very new to Scala. I am using IntelliJ IDE to run a very simple program in Scala(2.11.7). My program goes like

class Rational(n:Int,d:Int) {
  val oneHalf = new Rational(1,2)
}

I am trying to run it as a Class rather than a object. How could I Run this class in IntelliJ?

Thanks

avikkal
  • 11
  • 3
  • have you install scala plugin? – Maytham Fahmi Mar 04 '16 at 21:52
  • This class might cause problems - `oneHalf` is initialized when the constructor is called, but it calls the next constructor, and so on.. – Clashsoft Mar 04 '16 at 21:59
  • @maytham-ɯɐɥıλɐɯ Yes, I have installed a Scala plugin – avikkal Mar 04 '16 at 22:08
  • @Clashsoft : This is an example I took out of the book "Programming in Scala". So I am assuming it does not call the constructor recursively – avikkal Mar 04 '16 at 22:09
  • Mind you tell which page of the book? I agree with @Clashsoft that this is recursively called and you will run into issues. – yw3410 Mar 04 '16 at 22:21
  • I was to write this, which book and page will help to understand what you are asking for. – Maytham Fahmi Mar 04 '16 at 22:22
  • Its in page 131 of the book "Programming in Scala" - Martin Odersky, the solution provided by @maytham-ɯɐɥıλɐɯ helped in figuring out the solution I wanted. I think I was wrong about mentioning that "it does not call the constructor recursively" – avikkal Mar 04 '16 at 22:28
  • @avikkal: That's not the code that the book has. I tells you to create the class `class Rational(n: Int, d: Int) { println("Created "+ n +"/"+ d) }` and then create a new object of that class in the REPL – Falmarri Mar 04 '16 at 22:33
  • BTW welcome to SO. You got my welcome voteup – Maytham Fahmi Mar 04 '16 at 22:35
  • @Falmarri : That's right, it tells to create the class Rational, and I was trying to Run it in IntelliJ hoping I could print these val oneHalf = new Rational(1, 2) val twoThirds = new Rational(2, 3) (oneHalf / 7) + (1 twoThirds) but I wasn't able to Run it in IntelliJ, I had to create an object as mentioned by maytham-ɯɐɥıλɐɯ to get that program running. Is there an another way to Run it? – avikkal Mar 04 '16 at 22:44
  • If you like to work in intelliJ as alternative for REPL, just create Scala worksheet – Maytham Fahmi Mar 04 '16 at 23:02

1 Answers1

4

As @Clashsoft says you can not initialize the class like this. You can do some thing simple like this for testing:

class Rational(n: Int, d: Int) {
  def oneHalf: Int =
    n * d
}

object MyProgram {
  def main(args: Array[String]) {
    val rational = new Rational(1, 2)
    println(rational.oneHalf)

  }
}

It is also possible to use App trait (extends App) then you do not need to have main method:

object MyProgram extends App {
  val rational = new Rational(1, 2)
  println(rational.oneHalf)
}

All depends on how you want to implement your solution at the end. Regarding different between main and App trait please read more.

Thx to @tzachzohar for following addition:

In Scala, just like in Java, only a static main method (with appropriate argument and return types) can serve as a program's entry point. For convenience, IntelliJ IDEA provides a Scala Worksheet as a way to easily test your code, but that's no magic either - it's a just nice wrapper - behind the scenes, a worksheet has its own main method calling your code.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137