06 Mar 2010, 19:31
Matt-linked-in_pragsmall

Matthew Ueckerman (1 post)

Dear Venkat,

Can you explain why the companion object example (Marker.scala) on P.52 (hardcopy) cannot be loaded via an interactive shell? Here’s what I’m seeing…

$ scala Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17). Type in expressions to have them evaluated. Type :help for more information.

scala> :load Marker.scala Loading Marker.scala… defined class Marker

:7: error: constructor Marker cannot be accessed in object Marker "red" -> new Marker("red"), ^ :8: error: constructor Marker cannot be accessed in object Marker "blue" -> new Marker("blue"), ^ :9: error: constructor Marker cannot be accessed in object Marker "green" -> new Marker("green") Note, however, the file complies: $ scalac Marker.scala $ and executes when provided as an arg to scala: $ scala Marker.scala $ Kind Regards, Matthew
18 Mar 2010, 17:16
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Hi Matthew,

Sorry did not catch this question until now.

The reason is the scala REPL (interactive shell) is loading one line at a time. The companion object should be defined along with the class, and since it is line separated, the shell is not happy with it. To fix it (to work in shell), please modify Marker.scala from

… override def toString() : String = “marker color “ + color }

object Marker { …

to

… override def toString() : String = “marker color “ + color }; object Marker { …

Then ave the file and try to load it in the shell.

Hope this helps.

Regards,

Venkat

  You must be logged in to comment