0
  • The Rectangle module is defined but why the evaluation R.length and R.area is giving error : value area is not a member of Rectangle.
  • Any improvement in defining Rectangle module? Better code?

The Code:

class Rectangle(l:Double, w:Double)
{
    require (l>0, w>0)
    val length = l
    val width = w
    def this (l:Double) = this (l, l)
    def area = (length * width)
}

Scala Interpreter:

class Rectangle(l:Double, w:Double)


[parsing <console>]
[superaccessors in 7ms]
[selectiveanf in 0ms]
[erasure in 4ms]
[Generate ICode from the AST in 5ms]
[inliner in 0ms]
defined class Rectangle


val R = new Rectangle (12, 12)

[parsing <console>]
[erasure in 6ms]
[lazyvals in 0ms]
[Generating icode for <console>]
[Generate ICode from the AST in 4ms]
[inliner in 0ms]
R: Rectangle = Rectangle@117cc9d


val l = println("Rectangle length is " + R.length)

[parsing <console>]
[loaded class file /home/optimight/.eclipse/org.eclipse.platform_3.7.0_155965261/      configuration/org.eclipse.osgi/bundles/201/1/.cp/lib/scala-library.jar(scala/collection/TraversableOnce.class) in 0ms]
[loaded class file /home/optimight/.eclipse/org.eclipse.platform_3.7.0_155965261/configuration/org.eclipse.osgi/bundles/201/1/.cp/lib/scala-library.jar(scala/collection/immutable/IndexedSeq.class) in 0ms]
[total in 44ms]

val A = R.area

[parsing <console>]
<console>:9: error: value area is not a member of Rectangle

The image is showing complete code of Rectangle and scala interpreter in Eclipse Scala IDE.

Please guide.

Optimight
  • 2,809
  • 3
  • 25
  • 42

1 Answers1

2

After a quick look at the screenshot in hires (here), it seems that you don't really use the class you have defined in the interpreter. Either the Rectangle class is loaded when the interpreter start and you redefine it when you typed: class Rectangle(l: double, w: double). Either the class is not loaded and you have defined it for the first time.

In the first case you only have to avoid typing the first line in the interpreter, in the second case, you have to load the class into the interpreter (I'm not familiar with the scala IDE interpreter, so I can't tell you how to do it).

Nicolas
  • 23,373
  • 4
  • 57
  • 64
  • First I completed the coding of Rectangle class. Then ran the Scala Interpreter. – Optimight Jun 07 '12 at 15:50
  • Yes, but as explained in the answer, you've certainly override the definition of `Rectangle` with your first line in the interpreter. Can you try directly `val r = new Rectangle(12,12)` once the interpreter is loaded? – Nicolas Jun 07 '12 at 15:52
  • Maybe, the Scala Interpreter...is interpreting only one line not the entire class definition. How to run entire Class Definition? – Optimight Jun 07 '12 at 16:20
  • 1
    After logically thinking and exploring various options available in Eclipse Scala IDE for "How to run entire class definition and not only a single line as the Scala Interpreter (in the IDE) does, I found the actual process required. – Optimight Jun 07 '12 at 16:40
  • @Optimight, care to share? – James Moore Dec 08 '12 at 04:25
  • @James Moore, Please refer to the answer given by me. – Optimight Dec 08 '12 at 09:33