I'm using Intellij-idea for scala programming (with sbt plugin).
I want to know what is the difference between scala classes, scala scripts and scala worksheets. When we use each of them?
This will be very nice if you can explain it by a simple example.
Thanks
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.
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 .
On the Project pane on the left, right-click src and select New => Scala class. If you don't see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually.
You have different ways of running scala code:
First create a Program with your classes, this is as in java, I use object because it works well without instantianing, like static, just compile with the SBT and run it you can also use the scala Interpreter REPL
We can use this object in the REPL
scala> object Hello { | def main(args:Array[String]) { | println("Hello, Scala !!") | } | } defined object Hello scala> Hello.main(Array("onlyforwork")) Hello, Scala !!
compiling and running it using activator/SBT
> compile [info] Compiling 1 Scala source to /home/anquegi/Dev/StackOverFlow/scalaStack/target/scala-2.11/classes... [success] Total time: 2 s, completed 13/04/2015 11:29:42 > run [warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list Multiple main classes detected, select one to run: [1] org.example.Hello [2] org.example.ScheduledTaskScala [3] question1.Ques [4] scriptworksheet.Hello Enter number: 4 [info] Running scriptworksheet.Hello Hello, Scala !! [success] Total time: 19 s, completed 13/04/2015 11:30:04
The second is that if we add the scala code as a script or file Hello.scala, You can save your scala code in the file with .scala extension (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter
/** * Created by anquegi on 13/04/15. */ println("Hello, Scala !!")
if we call the scala interpreter this file is executed, you do not need to instanciate objects or clases, just executing like a shell script, you can also execute directlyy from Intellij, but I use the console with scala installed on the system
[anquegi@localhost scalaStack]$ scala src/main/scala/scriptworksheet/HelloScript.scala Hello, Scala !!
And finally the worksheet is the most powerfull, I recommend this for increasing your prodductivity at work bacause it is easy to test things is like the REPL, ant it evluates the scala exprssions and shows you back the result
Following is excerpt from official github repo wiki about the 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, auto-format, etc.
// We can define objects or classes and the worksheet //will print the sesult of every expression object Hello { def main(args:Array[String]) { println("Hello, Scala !!") } } println("Hello Scala") val a = 4 + 5
the result
defined module Hello Hello Scala res0: Unit = () a: Int = 9
then a capture that shows you working with classe the work sheet and the console for scriptsin the Intellij
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With