Scala工作表不评估IntelliJ IDEA 2016.2中的对象 [英] Scala worksheet does not evaluate object in IntelliJ IDEA 2016.2

查看:95
本文介绍了Scala工作表不评估IntelliJ IDEA 2016.2中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个非常简单的工作表内容演示了这个问题:

This very simple worksheet content demonstrates the issue:

object Test {
 println("This does not print!")
  add(5, 6)
}

println("This however prints!")
add(5, 6)

def add(a: Int, b: Int): Int = a + b

以上工作表内容的结果是:

Results from the above worksheet content are:

defined module Test

This however prints!
res0: Unit = ()
res1: Int = 11

add: add[](val a: Int,val b: Int) => Int

基于 JetBrains官方网站Scala工作表示例以及每个我发现的其他可靠资源(例如 Martin Odresky自己在Scala函数编程中的示例),我希望object Test的内容能够执行.我的软件是:

Based on JetBrains official website Scala Worksheets example and every other reliable resource I've found (like Martin Odresky's own examples in Functional Programming in Scala course), I would expect the contents of object Test to execute. My software is:

  • OS X El Capitan
  • IntelliJ IDEA 2016.2
  • SBT插件1.8.0
  • SBT版本0.13.12
  • Scala插件2016.2.1
  • Scala版本2.11.8

推荐答案

我认为这是您想要的:

object Test {
  println("This does not print!") 
  add(5, 6)

  println("This however prints!")
  add(5, 6)

  def add(a: Int, b: Int): Int = a + b
}

工作表的工作方式是,如果您定义一个对象,而在object范围之外没有定义任何对象,它将以Test extends App的形式执行. intellij页面显示的是什么

How the worksheet works is, that if you define an object and nothing defined outside the object scope, it will execute it as Test extends App. Which is what the intellij page displays

如果您在object范围之外有任何语句,则将其视为其他任何object,编译器将像其他任何语句一样对其进行初始化.这就是您所经历的.

If you have any statement outside the object scope, it is then treated as any other object and compiler will initialize it like anything else. This is what you are experiencing.

这篇关于Scala工作表不评估IntelliJ IDEA 2016.2中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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