Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between scala script and application

Tags:

scala

What is the difference between a scala script and scala application? Please provide an example

The book I am reading says that a script must always end in a result expression whereas the application ends in a definition. Unfortunately no clear example is shown.

Please help clarify this for me

like image 445
James Raitsev Avatar asked Dec 02 '12 21:12

James Raitsev


1 Answers

I think that what the author means is that a regular scala file needs to define a class or an object in order to work/be useful, you can't use top-level expressions (because the entry-points to a compiled file are pre-defined). For example:

println("foo")

object Bar {
    // Some code
}

The println statement is invalid in the top-level of a .scala file, because the only logical interpretation would be to run it at compile time, which doesn't really make sense.

Scala scripts in contrast can contain expressions on the top-level, because those are executed when the script is run, which makes sense again. If a Scala script file only contains definitions on the other hand, it would be useless as well, because the script wouldn't know what to do with the definitions. If you'd use the definitions in some way, however, that'd be okay again, e.g.:

object Foo {
    def bar = "test"
}

println(Foo.bar)

The latter is valid as a scala script, because the last statement is an expression using the previous definition, but not a definition itself.

like image 56
fresskoma Avatar answered Oct 27 '22 01:10

fresskoma