Reputation: 2876
I have encountered with something I think is a bug/no-syntax-sense in scala syntax but I prefer to post it here after reporting it as I might be wrong.
The controller class:
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 =>
}
}
}
The view class
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 = {
}
}
See the comment in the ItemController class. If I leave a blank line between the statement where the comment is and the newItem setVisible true
then the code gives no error.
Thanks in advance.
Upvotes: 1
Views: 132
Reputation: 53348
newItem eraseForm
newItem setVisible true
is parsed as
newItem.eraseForm(
newItem).setVisible(true);
but what you want to have is:
newItem.eraseForm;
newItem.setVisible(true);
Leaving out dots and parentheses means that the Scala compiler applies "operator notation". This means that your expression always needs to be of the form obj meth param
. It is possible to have obj meth;
but as you can see this form needs to be stopped by a semicolon. You can either insert it by yourself or let the compiler infer it by adding an empty line, an expression that starts with a keyword in the following line or with closing braces and parentheses.
Since 2.10 you will also get a warning if you write obj meth
because such postfix operators can be harmful as you can see with your question.
See these two question and their answers for more information on the "operator notation" rule:
Upvotes: 5