Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between object with main() and extends App in scala
    text
    copied!<p>I'm working through ScalaInAction (book is still a MEAP, but code is public on github) Right now I'm in chapter 2 looking at this restClient: : <a href="https://github.com/nraychaudhuri/scalainaction/blob/master/chap02/RestClient.scala" rel="nofollow">https://github.com/nraychaudhuri/scalainaction/blob/master/chap02/RestClient.scala</a></p> <p>First, I setup intelliJ with scala extensions and created a HelloWorld with <code>main()</code>:</p> <pre><code>&lt;ALL the imports&gt; object HelloWorld { def main(args: Array[String]) { &lt;ALL the rest code from RestClient.scala&gt; } } </code></pre> <p>I get the following error when compiling:</p> <pre><code>scala: forward reference extends over defintion of value command val httppost = new HttpPost(url) ^ </code></pre> <p>I can fix this by moving the following lines around until the ordering is correct with relation to the <code>def</code>'s</p> <pre><code>require( args.size &gt;= 2, "You need at least two arguments to make a get, post, or delete request") val command = args.head val params = parseArgs(args) val url = args.last command match { case "post" =&gt; handlePostRequest case "get" =&gt; handleGetRequest case "delete" =&gt; handleDeleteRequest case "options" =&gt; handleOptionsRequest } </code></pre> <p>While browsing the github page, I found this: <a href="https://github.com/nraychaudhuri/scalainaction/tree/master/chap02/restclient" rel="nofollow">https://github.com/nraychaudhuri/scalainaction/tree/master/chap02/restclient</a></p> <p>Which uses implements RestClient.scala using <code>extends App</code> instead of a <code>main()</code> method:</p> <pre><code>&lt;All the imports&gt; object RestClient extends App { &lt;All the rest of the code from RestClient.scala&gt; } </code></pre> <p>I then changed my <code>object HelloWorld</code> to just use <code>extends App</code> instead of implementing a <code>main()</code> method and it works without errors</p> <p>Why does the <code>main()</code> method way of doing this generate the error but the <code>extends App</code> does not? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload