Difference between object with main() and extends App in scala

不羁岁月 提交于 2021-02-07 08:49:35

问题


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: : https://github.com/nraychaudhuri/scalainaction/blob/master/chap02/RestClient.scala

First, I setup intelliJ with scala extensions and created a HelloWorld with main():

<ALL the imports>

object HelloWorld {
   def main(args: Array[String]) {
     <ALL the rest code from RestClient.scala>
   }
}

I get the following error when compiling:

scala: forward reference extends over defintion of value command
val httppost = new HttpPost(url)
                ^

I can fix this by moving the following lines around until the ordering is correct with relation to the def's

require( args.size >= 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"    => handlePostRequest
  case "get"     => handleGetRequest
  case "delete"  => handleDeleteRequest
  case "options" => handleOptionsRequest
}

While browsing the github page, I found this: https://github.com/nraychaudhuri/scalainaction/tree/master/chap02/restclient

Which uses implements RestClient.scala using extends App instead of a main() method:

<All the imports>
object RestClient extends App {
   <All the rest of the code from RestClient.scala>
}

I then changed my object HelloWorld to just use extends App instead of implementing a main() method and it works without errors

Why does the main() method way of doing this generate the error but the extends App does not?


回答1:


Because main() is a method, and variable's in method could not be forward reference.

For example:

object Test {

   // x, y is object's instance variable, it could be forward referenced

   def sum = x + y // This is ok
   val y = 10    
   val x = 10

}

But code in a method could not forward referenced.

object Test {
    def sum = {
        val t = x + y // This is not ok, you don't have x, y at this point
        val x = 10
        val y = 20
        val z = x + y // This is ok
    }
}

In your case, if you copy paste all codes from RestClient.scala to main(), you will have the same issue because var url is declared after its usage in handlePostRequest.



来源:https://stackoverflow.com/questions/14553461/difference-between-object-with-main-and-extends-app-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!