Scala中带有main()和extends App的对象的区别 [英] Difference between object with main() and extends App in scala

查看:38
本文介绍了Scala中带有main()和extends App的对象的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 ScalaInAction(书仍然是 MEAP,但代码在 github 上是公开的)现在我在第 2 章看这个 restClient::https:///github.com/nraychaudhuri/scalinaction/blob/master/chap02/RestClient.scala

首先,我使用 Scala 扩展设置了 intelliJ 并使用 main() 创建了一个 HelloWorld:

<所有导入>对象你好世界{def main(args: Array[String]) {<来自 RestClient.scala 的所有其余代码>}}

编译时出现以下错误:

scala:前向引用扩展值命令的定义val httppost = 新的 HttpPost(url)^

我可以通过移动以下几行来解决这个问题,直到相对于 def 的顺序是正确的

require( args.size >= 2, "你至少需要两个参数来进行获取、发布或删除请求")val 命令 = args.headval 参数 = parseArgs(args)val url = args.last命令匹配{案例发布"=>处理后请求案例获取"=>处理获取请求案例删除"=>处理删除请求案例选项"=>处理选项请求}

在浏览 github 页面时,我发现了这个:https://github.com/nraychaudhuri/scalinaction/tree/master/chap02/restclient

使用 extends App 而不是 main() 方法实现 RestClient.scala:

<所有导入>对象 RestClient 扩展应用程序 {<来自 RestClient.scala 的所有其余代码>}

然后我将我的 object HelloWorld 更改为仅使用 extends App 而不是实现 main() 方法并且它可以正常工作>

为什么执行此操作的 main() 方法会产生错误,而 extends App 不会?

解决方案

因为 main() 是一个方法,变量的 in 方法不能被前向引用.

例如:

对象测试{//x, y 是对象的实例变量,可以向前引用def sum = x + y//没问题值 y = 10值 x = 10}

但是方法中的代码无法向前引用.

对象测试{定义总和 = {val t = x + y//这不行,此时你没有 x, y值 x = 10值 y = 20val z = x + y//没问题}}

在您的情况下,如果您将所有代码从 RestClient.scala 复制粘贴到 main(),您将遇到同样的问题,因为 var url 在 handlePostRequest 中使用后声明.

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?

解决方案

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.

这篇关于Scala中带有main()和extends App的对象的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆