Scala语法错误?

时间:2013-08-23 19:29:31

标签: scala syntax

我在scala语法中遇到了一些我认为是bug / no-syntax-sense的东西,但我更喜欢在报告之后将其发布在此处,因为我可能错了。

控制器类:

package some

import java.awt.event.ActionListener
import java.awt.event.ActionEvent

class ItemController extends ActionListener {

  val NEW_ITEM = "new item"
  val UPDATE_ITEM = "update item"

  private val newItem = new ItemNew

  override def actionPerformed(e: ActionEvent) = {

  }

  def create(view: String) = {

    view match {
      case NEW_ITEM => {
        newItem eraseForm //Here eclipse says "Unit does not take parameters".
        newItem setVisible true
      }

      case UPDATE_ITEM =>
    }

  }

}

视图类

  package some

  import scala.collection.mutable.HashMap
  import javax.swing.JPanel
  import java.awt.GridLayout
  import javax.swing.JFrame
  import scala.collection.mutable.LinkedHashMap
  import javax.swing.JTextField
  import java.awt.BorderLayout
  import java.awt.FlowLayout
  import javax.swing.JLabel

  class ItemNew extends JFrame {

    private val formFields = LinkedHashMap[String, JTextField]()

    init

    private def init(): Unit = {
      // defining form fields
      formFields += ("name" -> new JTextField(20),
        "surname" -> new JTextField(20),
        "age" -> new JTextField(3))

      setLayout(new BorderLayout)

      val formPanel = new JPanel
      formPanel setLayout (new GridLayout(formFields.size, 1))

      generateForm(formPanel)
      getContentPane add formPanel

      pack
      setVisible(true)

      def generateForm(formPanel: JPanel) = {

        var leftPanel: JPanel = null
        var rightPanel: JPanel = null
        var gridPanel: JPanel = null

        formFields foreach (entry => {

          leftPanel = new JPanel(new FlowLayout)
          leftPanel.getLayout().asInstanceOf[FlowLayout] setAlignment FlowLayout.LEFT

          rightPanel = new JPanel(new FlowLayout)
          rightPanel.getLayout.asInstanceOf[FlowLayout] setAlignment FlowLayout.LEFT

          gridPanel = new JPanel(new GridLayout(1, 2))

          leftPanel add new JLabel(entry._1 + ':')
          rightPanel add entry._2

          gridPanel add leftPanel
          gridPanel add rightPanel

          formPanel add gridPanel

        })
      }
    }

    def getFormContent: HashMap[String, String] = {

      val formData = HashMap[String, String]()

      formFields foreach (entry => {
        formData += (entry._1 -> entry._2.getText)
      })

      return formData
    }

    def eraseForm : Unit = {
      formFields foreach (entry => {
        entry._2 setText ""
      })
    }

    private def addListeners = {

    }

  }

请参阅ItemController类中的注释。如果我在注释所在的语句和newItem setVisible true之间留下一个空行,那么代码就不会出错。

提前致谢。

1 个答案:

答案 0 :(得分:5)

newItem eraseForm
newItem setVisible true

被解析为

newItem.eraseForm(
newItem).setVisible(true);

但你想拥有的是:

newItem.eraseForm;
newItem.setVisible(true);

省略点和圆括号表示Scala编译器应用“运算符表示法”。这意味着您的表达式始终必须采用obj meth param形式。可以有obj meth;,但正如您所见,此表单需要以分号停止。你可以自己插入它,也可以让编译器通过添加一个空行,一个以下一行中的关键字开头的表达式或者用括号括号和圆括号来推断它。

从2.10开始,如果你写obj meth,你也会收到警告,因为这样的后缀操作符可能会对你的问题有所了解。

有关“操作符号”规则的更多信息,请参阅这两个问题及其答案: