Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij worksheet and classes defined in it

I'm following along the Coursera course on functional programming in Scala and came along a weird behavior of the worksheet repl.

In the course a worksheet with the following code should give the following results on the right:

object rationals {
val x = new Rational(1, 2)              > x : Rational = Rational@<hash_code>
   x.numer                              > res0: Int = 1
   y. denom                             > res1: Int = 2
}

class Rational(x: Int, y: Int) {
   def numer = x
   def denom = y
}

What I get is

object rationals {                      > defined module rationals
val x = new Rational(1, 2)              
   x.numer                              
   y. denom                             
}

class Rational(x: Int, y: Int) {        > defined class Rational
   def numer = x
   def denom = y
}

Only after moving the class into the object I got the same result as in the code.

  • Is this caused by Intellij, or have there been changes in Scala?
  • Are there other ways around this?
like image 953
Lukasz Avatar asked Nov 10 '15 12:11

Lukasz


People also ask

What is Scala worksheet in IntelliJ?

A worksheet is a scala file with . sc extension which you can run and get evaluation results in a special view appeared in the editor. Create worksheet by right-clicking on your Project and choosing 'New' -> 'Scala Worksheet'. Just type your code, press 'Evaluate worksheet' button and results appear.

What is a Scala worksheet?

A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. Worksheets are like a REPL session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, etc. Worksheet use the extension .

Can you run Scala in IntelliJ?

Run Scala applications You can run your Scala code through IntelliJ IDEA, use sbt shell, or use Scala worksheet for a quick code evaluation.


1 Answers

In the IntelliJ IDEA scala worksheet handles values inside the objects differently than Eclipse/Scala IDE.

Values inside objects are not evaluated in linear sequence mode, instead they are treated as normal scala object. You barely see information about it until explicit use.

To actually see your vals and expressions simply define or evaluate them outside any object\class

This behaviour could be a saviour in some cases. Suppose you have that definitions.

  val primes = 2l #:: Stream.from(3, 2).map(_.toLong).filter(isPrime)

  val isPrime: Long => Boolean = 
    n => primes.takeWhile(p => p * p <= n).forall(n % _ != 0)

Note that isPrime could be a simple def, but we choose to define it as val for some reason.

Such code is nice and working in any normal scala code, but will fail in the worksheet, because vals definitions are cross-referencing.

But it you wrap such lines inside some object like

object Primes {
  val primes = 2l #:: Stream.from(3, 2).map(_.toLong).filter(isPrime)

  val isPrime: Long => Boolean =
    n => primes.takeWhile(p => p * p <= n).forall(n % _ != 0)
}

It will be evaluated with no problem

like image 171
Odomontois Avatar answered Sep 23 '22 13:09

Odomontois