{"id":592,"date":"2011-10-31T23:46:00","date_gmt":"2011-10-31T23:46:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/scala-tutorial-scripting-compiling-main-methods-return-values-of-functions.html"},"modified":"2012-10-21T20:22:05","modified_gmt":"2012-10-21T20:22:05","slug":"scala-tutorial-scripting-compiling-main","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html","title":{"rendered":"Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">\n<h2>  Preface<\/h2>\n<p>This is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on <a href=\"http:\/\/icl-f11.utcompling.com\/links\">the links page of the Computational Linguistics course<\/a> I\u2019m creating these for. Additionally you can find this and other tutorial series on the JCG Java <a href=\"http:\/\/www.javacodegeeks.com\/p\/java-tutorials.html\">Tutorials page<\/a>.<\/p>\n<p>The tutorials up to this point have been based on working with the Scala REPL or running basic scripts that are run from the command line. The latter is called \u201cscripting\u201d and usually is done for fairly simple, self-contained coding tasks. For more involved tasks that require a number of different modules and accessing libraries produced by others, it is necessary to work with a build system that brings together your code, others\u2019 code, allows you to compile it, test it, and package it so that you can use it as an application.<\/p>\n<p>This tutorial takes you from running Scala scripts to compiling Scala programs to create byte code that can be shared by different applications. This will act as a bridge to set you up for the next step of using a build system. Along the way, some points will be made about objects, extending on some of the ideas from the previous tutorial about object-oriented programming. At a high level, the relevance of objects to a larger, modularized code base should be pretty clear: objects encapsulate data and functions that can be used by other objects, and we need to be able to organize them so that objects know how to find other objects and class definitions. Build systems, which we\u2019ll look at in the next tutorial, will make this straightforward.<\/p>\n<h2>  Running Scala scripts<\/h2>\n<p>In the beginning, you started with the REPL.<\/p>\n<pre class=\"brush: scala;\">scala&gt; println(\"Hello, World!\")\r\nHello, World!\r\n<\/pre>\n<p>Of course, the REPL is just a (very useful) playground for trying out snippets of Scala code, not for doing real work. So, you saw that you could put code like <strong>println(\u201cHello, World!\u201d)<\/strong> into a file called <strong>Hello.scala<\/strong> and run it from the command line.<\/p>\n<pre class=\"brush: bash;\">$ scala Hello.scala\r\nHello, World!\r\n<\/pre>\n<p>The homeworks and tutorials done so far have worked in this way, though they are a bit more complex. We can even include class definitions and objects created from a class. For example, using the <strong>Person<\/strong> class from <a href=\"http:\/\/bcomposes.wordpress.com\/2011\/10\/24\/first-steps-in-scala-for-beginning-programmers-part-9\/\" title=\"First steps in Scala for beginning programmers, Part 9\">the previous tutorial<\/a>, we can put all the code into a file called <strong>People.scala<\/strong> (btw, this name doesn\u2019t matter \u2014 could as well be <strong>Blurglecruncheon.scala<\/strong>).<\/p>\n<pre class=\"brush: scala;\">class Person (\r\n  val firstName: String,\r\n  val lastName: String,\r\n  val age: Int,\r\n  val occupation: String\r\n) {\r\n \r\n  def fullName: String = firstName + \" \" + lastName\r\n \r\n  def greet (formal: Boolean): String = {\r\n    if (formal)\r\n      \"Hello, my name is \" + fullName + \". I'm a \" + occupation + \".\"\r\n    else\r\n      \"Hi, I'm \" + firstName + \"!\"\r\n  }\r\n \r\n}\r\n \r\nval johnSmith = new Person(\"John\", \"Smith\", 37, \"linguist\")\r\nval janeDoe = new Person(\"Jane\", \"Doe\", 34, \"computer scientist\")\r\nval johnDoe = new Person(\"John\", \"Doe\", 43, \"philosopher\")\r\nval johnBrown = new Person(\"John\", \"Brown\", 28, \"mathematician\")\r\n \r\nval people = List(johnSmith, janeDoe, johnDoe, johnBrown)\r\npeople.foreach(person =&gt; println(person.greet(true)))\r\n<\/pre>\n<p>This can now be run from the command line, producing the expected result.<\/p>\n<pre class=\"brush: bash;\">$ scala People.scala\r\nHello, my name is John Smith. I'm a linguist.\r\nHello, my name is Jane Doe. I'm a computer scientist.\r\nHello, my name is John Doe. I'm a philosopher.\r\nHello, my name is John Brown. I'm a mathematician.\r\n<\/pre>\n<p>However, suppose you wanted to use the <strong>Person<\/strong> class from a different application (e.g. that is defined in a different file). You might think you could save the following in the file <strong>Radiohead.scala<\/strong>, and then run it with Scala.<\/p>\n<pre class=\"brush: scala;\">val thomYorke = new Person(\"Thom\", \"Yorke\", 43, \"musician\")\r\nval johnnyGreenwood = new Person(\"Johnny\", \"Greenwood\", 39, \"musician\")\r\nval colinGreenwood = new Person(\"Colin\", \"Greenwood\", 41, \"musician\")\r\nval edObrien = new Person(\"Ed\", \"O'Brien\", 42, \"musician\")\r\nval philSelway = new Person(\"Phil\", \"Selway\", 44, \"musician\")\r\nval radiohead = List(thomYorke, johnnyGreenwood, colinGreenwood, edObrien, philSelway)\r\nradiohead.foreach(bandmember =&gt; println(bandmember.greet(false)))\r\n<\/pre>\n<p>However, if you do \u201c<strong>scala Radiohead.scala<\/strong>\u201d you\u2019ll see five errors, each one complaining that the type <strong>Person<\/strong> wasn\u2019t found. How could <strong>Radiohead.scala<\/strong> know about the Person class and where to find its definition? I\u2019m not aware of a way to do this with scripting-style Scala programming, and even though I suspect there may be a way to do something this simple, I don\u2019t even care to know it. Let\u2019s just get straight to compiling.<\/p>\n<h2>  Compiling<\/h2>\n<p>The usual thing we do with Scala is to compile our programs to byte code. We won\u2019t go into the details of that, but it basically means that Scala turns the text of a Scala program into a compiled set of machine instructions that can be interpreted by your operating system. (It actually compiles to Java byte code, which is one reason it is pretty straightforward to use Java code when coding in Scala.)<\/p>\n<p>So, what does compilation look like? We need to start by changing the code we did above a bit. Make a directory that has nothing in it, say <strong>\/tmp\/tutorial<\/strong>. Then save the following as <strong>PersonApp.scala<\/strong> in that directory.<\/p>\n<pre class=\"brush: scala;\">class Person (\r\n  val firstName: String,\r\n  val lastName: String,\r\n  val age: Int,\r\n  val occupation: String\r\n) {\r\n \r\n  def fullName: String = firstName + \" \" + lastName\r\n \r\n  def greet (formal: Boolean): String = {\r\n    if (formal)\r\n      \"Hello, my name is \" + fullName + \". I'm a \" + occupation + \".\"\r\n    else\r\n      \"Hi, I'm \" + firstName + \"!\"\r\n  }\r\n \r\n}\r\n \r\nobject PersonApp {\r\n \r\n  def main (args: Array[String]) {\r\n    val johnSmith = new Person(\"John\", \"Smith\", 37, \"linguist\")\r\n    val janeDoe = new Person(\"Jane\", \"Doe\", 34, \"computer scientist\")\r\n    val johnDoe = new Person(\"John\", \"Doe\", 43, \"philosopher\")\r\n    val johnBrown = new Person(\"John\", \"Brown\", 28, \"mathematician\")\r\n \r\n    val people = List(johnSmith, janeDoe, johnDoe, johnBrown)\r\n    people.foreach(person =&gt; println(person.greet(true)))\r\n  }\r\n \r\n}\r\n<\/pre>\n<p>Notice that the code looks pretty similar to the script above, but now we have a <strong>PersonApp<\/strong> object with a <strong>main<\/strong> method. The <strong>main<\/strong> method contains all the stuff that the original script had after the <strong>Person<\/strong> definition. Notice also that there is an <strong>args<\/strong> argument to the <strong>main<\/strong> method, which should look familiar now. What you are seeing is that a Scala script is basically just a simplified view of an object with a <strong>main<\/strong> method. Such scripts use the convention that the <strong>Array[String]<\/strong> provided to the method is called <strong>args<\/strong>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Okay, so now consider what happens if you run \u201c<strong>scala PersonApp.scala<\/strong>\u201d \u2014 nothing at all. That\u2019s because there is no executable code available outside of the object and class definitions. Instead, we need to compile the code and then run the <strong>main<\/strong> method of specific objects. The next step is to run <strong>scalac<\/strong> (N.B. \u201cscala<strong>c<\/strong>\u201d with a \u201cc\u201d, not \u201cscala\u201d) on <strong>PersonApp.scala<\/strong>. The name scalac is short for <span style=\"text-decoration: underline\">Scala c<\/span>ompiler. Do the following steps in the <strong>\/tmp\/tutorial<\/strong> directory.<\/p>\n<pre class=\"brush: bash;\">$ scalac PersonApp.scala\r\n$ ls\r\nPerson.class                    PersonApp.class\r\nPersonApp$$anonfun$main$1.class PersonApp.scala\r\nPersonApp$.class\r\n<\/pre>\n<p>Notice that a number of <strong>*.class<\/strong> files have been generated. These are byte code files that the scala application is able to run. A nice thing here is that it all the compilation is done: when in the past you ran \u201cscala\u201d on your programs (scripts), it had to first compile the instructions and then run the program. Now we are separating these steps into a compilation phase and a running phase.<\/p>\n<p>Having generated the class files, we can run any object that has a main method, like <strong>PersonApp<\/strong>.<\/p>\n<pre class=\"brush: bash;\">$ scala PersonApp\r\nHello, my name is John Smith. I'm a linguist.\r\nHello, my name is Jane Doe. I'm a computer scientist.\r\nHello, my name is John Doe. I'm a philosopher.\r\nHello, my name is John Brown. I'm a mathematician.\r\n<\/pre>\n<p>Try running \u201c<strong>scala Person<\/strong>\u201d to see the error message it gives you.<\/p>\n<p>Next, move the <strong>Radiohead.scala<\/strong> script that you saved earlier into this directory and run it.<\/p>\n<pre class=\"brush: bash;\">$ scala Radiohead.scala\r\nHi, I'm Thom!\r\nHi, I'm Johnny!\r\nHi, I'm Colin!\r\nHi, I'm Ed!\r\nHi, I'm Phil!\r\n<\/pre>\n<p>This is the same script, but now it is in a directory that contains the <strong>Person.class<\/strong> file, which tells Scala everything that <strong>Radiohead.scala<\/strong> needs to construct objects of the <strong>Person<\/strong> class. Scala makes available any class file that is defined in the <em>CLASSPATH<\/em>, an environment variable that by default includes the current working directory.<\/p>\n<p>Despite this success, we\u2019re going away from script land with this post, so change the contents of <strong>Radiohead.scala<\/strong> to be the following.<\/p>\n<pre class=\"brush: scala;\">object RadioheadGreeting {\r\n \r\n  def main (args: Array[String]) {\r\n    val thomYorke = new Person(\"Thom\", \"Yorke\", 43, \"musician\")\r\n    val johnnyGreenwood = new Person(\"Johnny\", \"Greenwood\", 39, \"musician\")\r\n    val colinGreenwood = new Person(\"Colin\", \"Greenwood\", 41, \"musician\")\r\n    val edObrien = new Person(\"Ed\", \"O'Brien\", 42, \"musician\")\r\n    val philSelway = new Person(\"Phil\", \"Selway\", 44, \"musician\")\r\n    val radiohead = List(thomYorke, johnnyGreenwood, colinGreenwood, edObrien, philSelway)\r\n    radiohead.foreach(bandmember =&gt; println(bandmember.greet(false)))\r\n  }\r\n \r\n}\r\n<\/pre>\n<p>Then run scalac on all of the <strong>*.scala<\/strong> files in the directory. There are now more class files, corresponding to the <strong>RadioheadGreeting<\/strong> object we defined.<\/p>\n<pre class=\"brush: bash;\">$ scalac *.scala\r\n$ ls\r\nPerson.class                            Radiohead.scala\r\nPersonApp$$anonfun$main$1.class         RadioheadGreeting$$anonfun$main$1.class\r\nPersonApp$.class                        RadioheadGreeting$.class\r\nPersonApp.class                         RadioheadGreeting.class\r\nPersonApp.scala\r\n<\/pre>\n<p>You can now run \u201c<strong>scala RadioheadGreeting<\/strong>\u201d to get the greeting from the band members. Notice that the file <strong>RadioheadGreeting<\/strong> was saved in was called <strong>Radiohead.scala<\/strong> and that no class files were generated called <strong>Radiohead.class<\/strong>, etc. Again, the file name could have been named something entirely different, like <strong>Turlingdrome.scala<\/strong>. (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Vogon#Poetry\">Embrace your inner Vogon.<\/a>)<\/p>\n<h2>  Multiple objects in the same file<\/h2>\n<p>There is no problem having multiple objects with <strong>main<\/strong> methods in the same file. When you compile the file with <strong>scalac<\/strong>, each object generates its own set of class files, and you call <strong>scala<\/strong> on whichever class file contains the definition for the <strong>main<\/strong> method you want to run. As an example, save the following as <strong>Greetings.scala<\/strong>.<\/p>\n<pre class=\"brush: scala;\">object Hello {\r\n  def main (args: Array[String]) {\r\n    println(\"Hello, world!\")\r\n  }\r\n}\r\n \r\nobject Goodbye {\r\n  def main (args: Array[String]) {\r\n    println(\"Goodbye, world!\")\r\n  }\r\n}\r\n \r\nobject SayIt {\r\n  def main (args: Array[String]) {\r\n    args.foreach(println)\r\n  }\r\n}\r\n<\/pre>\n<p>Next compile the file and then you can run any of the generated class files (since they all have <strong>main<\/strong> methods).<\/p>\n<pre class=\"brush: bash;\">$ scalac Greetings.scala\r\n$ scala Hello\r\nHello, world!\r\n$ scala Goodbye\r\nGoodbye, world!\r\n$ scala Goodbye many useless arguments\r\nGoodbye, world!\r\n$ scala SayIt \"Oh freddled gruntbuggly\" \"thy micturations are to me\" \"As plurdled gabbleblotchits on a lurgid bee.\"\r\nOh freddled gruntbuggly\r\nthy micturations are to me\r\nAs plurdled gabbleblotchits on a lurgid bee.\r\n<\/pre>\n<p>In case you missed it earlier, the <strong>args<\/strong> array is where the command line arguments go and you can thus make use of them (or not, as in the case of the <strong>Hello<\/strong> and <strong>Goodbye<\/strong> objects).<\/p>\n<h2>  Functions with return values versus those without<\/h2>\n<p>Some functions return a value while others do not. As a simple example, consider the following pairs of functions.<\/p>\n<pre class=\"brush: scala;\">scala&gt; def plusOne (x: Int) = x+1\r\nplusOne: (x: Int)Int\r\n \r\nscala&gt; def printPlusOne (x: Int) = println(x+1)\r\nprintPlusOne: (x: Int)Unit\r\n<\/pre>\n<p>The first takes an <strong>Int<\/strong> argument and returns an <strong>Int<\/strong>, which is a value. The other takes an <strong>Int<\/strong> and returns <strong>Unit<\/strong>, which is to say it doesn\u2019t return a value. Notice the difference in behavior between the two following uses of the functions.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val foo = plusOne(2)\r\nfoo: Int = 3\r\n \r\nscala&gt; val bar = printPlusOne(2)\r\n3\r\nbar: Unit = ()\r\n<\/pre>\n<p>Scala uses a slightly subtle distinction in function definitions that can distinguish functions that return values versus those that return <strong>Unit<\/strong> (no value): If you don\u2019t use an equals sign in the definition, it means that the function returns <strong>Unit<\/strong>.<\/p>\n<pre class=\"brush: scala;\">scala&gt; def plusOneNoEquals (x: Int) { x+1 }\r\nplusOneNoEquals: (x: Int)Unit\r\n \r\nscala&gt; def printPlusOneNoEquals (x: Int) { println(x+1) }\r\nprintPlusOneNoEquals: (x: Int)Unit\r\n<\/pre>\n<p>Notice that the above definition of <strong>plusOneNoEquals<\/strong> returns <strong>Unit<\/strong>, even though it looks almost identical to <strong>plusOne<\/strong> defined earlier. Check it out.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val foo = plusOneNoEquals(2)\r\nfoo: Unit = ()\r\n<\/pre>\n<p>Now look back at the <strong>main<\/strong> methods given earlier. No equals. Yep, they don\u2019t have a return value. They are the entry point into your code, and any effects of running the code must be output to the console (e.g. with <strong>println<\/strong> or via a GUI) or written to the file system (or the internet somewhere). The outputs of such functions (ones which do not return a value) are called side-effects. You need them for the main methods. However, in many styles of programming, a great deal of work is done with side-effects. I\u2019ve been trying to gently lead the readers of this tutorial to adopt a more functional approach that tries to avoid them. I\u2019ve found it a more effective style myself in my own coding, so I\u2019m hoping it will serve you all better to start from that point. (Note that Scala supports many styles of programming, which is nice because you have choice and can go with what you find most suitable.)<\/p>\n<h2>  Cleaning up<\/h2>\n<p>You may have noticed that the directory you are working in as you run <strong>scalac<\/strong> on your scala files becomes quite littered with class files. For example, here\u2019s what the state of the code directory worked with in this tutorial looks like after compiling all files.<\/p>\n<pre class=\"brush: bash;\">$ ls\r\nGoodbye$.class                          PersonApp.scala\r\nGoodbye.class                           Radiohead.scala\r\nGreetings.scala                         RadioheadGreeting$$anonfun$main$1.class\r\nHello$.class                            RadioheadGreeting$.class\r\nHello.class                             RadioheadGreeting.class\r\nPerson.class                            SayIt$$anonfun$main$1.class\r\nPersonApp$$anonfun$main$1.class         SayIt$.class\r\nPersonApp$.class                        SayIt.class\r\nPersonApp.class\r\n<\/pre>\n<p>A mess, right? Generally, one would rarely develop a Scala application by compiling it directly in this way. Instead a build system is used to manage the compilation process, organize the files, and allow one to easily access software libraries created by other developers. The next tutorial will cover this, using <a href=\"https:\/\/github.com\/harrah\/xsbt\/wiki\">SBT <\/a>(the Simple Build Tool).<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/bcomposes.wordpress.com\/2011\/10\/25\/first-steps-in-scala-for-beginning-programmers-part-10\/\">First steps in Scala for beginning programmers, Part 10<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Jason Baldridge at the <a href=\"http:\/\/bcomposes.wordpress.com\/\">Bcomposes<\/a> blog.<\/p>\n<p><strong><i>Related Articles :<\/i><\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-scala-repl-expressions.html\">Scala Tutorial &#8211; Scala REPL, expressions, variables, basic types, simple functions, saving and running programs, comments<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html\">Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-conditional-execution.html\">Scala Tutorial &#8211; conditional execution with if-else blocks and matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-iteration-for.html\">Scala Tutorial &#8211; iteration, for expressions, yield, map, filter, count<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions.html\">Scala Tutorial &#8211; regular expressions, matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions_05.html\">Scala Tutorial &#8211; regular expressions, matching and substitutions with the scala.util.matching API<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-maps-sets-groupby.html\">Scala Tutorial &#8211; Maps, Sets, groupBy, Options, flatten, flatMap<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scalaiosource-accessing.html\">Scala Tutorial &#8211; scala.io.Source, accessing files, flatMap, mutable Maps<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html\">Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-sbt-scalabha-packages.html\">Scala Tutorial &#8211; SBT, scalabha, packages, build systems<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html\">Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/fun-with-function-composition-in-scala.html\">Fun with function composition in Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/08\/how-scala-changed-way-i-think-about-my.html\">How Scala changed the way I think about my Java Code<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/testing-with-scala.html\">Testing with Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/12\/things-every-programmer-should-know.html\">Things Every Programmer Should Know<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Preface This is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on the links page of the Computational Linguistics course I\u2019m creating these for. Additionally you can find this and other tutorial series on the JCG Java &hellip;<\/p>\n","protected":false},"author":67,"featured_media":227,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[235],"class_list":["post-592","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-scala-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-31T23:46:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:22:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Baldridge\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/jasonbaldridge\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Baldridge\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html\"},\"author\":{\"name\":\"Jason Baldridge\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\"},\"headline\":\"Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions\",\"datePublished\":\"2011-10-31T23:46:00+00:00\",\"dateModified\":\"2012-10-21T20:22:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html\"},\"wordCount\":1811,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"keywords\":[\"Scala Tutorial\"],\"articleSection\":[\"Scala\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html\",\"name\":\"Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"datePublished\":\"2011-10-31T23:46:00+00:00\",\"dateModified\":\"2012-10-21T20:22:05+00:00\",\"description\":\"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-scripting-compiling-main.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Scala\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\\\/scala\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\",\"name\":\"Jason Baldridge\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"caption\":\"Jason Baldridge\"},\"sameAs\":[\"http:\\\/\\\/bcomposes.wordpress.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/pub\\\/jason-baldridge\\\/5\\\/629\\\/9b2\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/jasonbaldridge\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/jason-baldridge\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks","description":"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks","og_description":"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","og_url":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-10-31T23:46:00+00:00","article_modified_time":"2012-10-21T20:22:05+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","type":"image\/jpeg"}],"author":"Jason Baldridge","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/jasonbaldridge","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jason Baldridge","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html"},"author":{"name":"Jason Baldridge","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0"},"headline":"Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions","datePublished":"2011-10-31T23:46:00+00:00","dateModified":"2012-10-21T20:22:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html"},"wordCount":1811,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","keywords":["Scala Tutorial"],"articleSection":["Scala"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html","url":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html","name":"Scala Tutorial - scripting, compiling, main methods, return values of functions - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","datePublished":"2011-10-31T23:46:00+00:00","dateModified":"2012-10-21T20:22:05+00:00","description":"PrefaceThis is part 10 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages"},{"@type":"ListItem","position":3,"name":"Scala","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages\/scala"},{"@type":"ListItem","position":4,"name":"Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0","name":"Jason Baldridge","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","caption":"Jason Baldridge"},"sameAs":["http:\/\/bcomposes.wordpress.com\/","http:\/\/www.linkedin.com\/pub\/jason-baldridge\/5\/629\/9b2","https:\/\/x.com\/http:\/\/twitter.com\/jasonbaldridge"],"url":"https:\/\/www.javacodegeeks.com\/author\/jason-baldridge"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}