Reputation: 6139
Can you please help me understand, why the second def
is not compiling, and how to write the second def
for that it is compiling (return first entry of Array in one line). Thanks!
import java.awt.{GraphicsDevice, GraphicsEnvironment}
class SeparatedTestCase {
def does_compile: GraphicsDevice = {
val ge: GraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment
val devices: Array[GraphicsDevice] = ge.getScreenDevices
devices(0)
}
def does_not_compile: GraphicsDevice = {
val ge: GraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment
val device0: GraphicsDevice = (ge.getScreenDevices)(0) // <---- compile error
device0
}
}
//Error:(13, 59) no arguments allowed for nullary method getScreenDevices: ()Array[java.awt.GraphicsDevice]
//val device0: GraphicsDevice = (ge.getScreenDevices)(0)
Upvotes: 1
Views: 47
Reputation: 127741
You have to invoke the method with explicit parentheses:
ge.getScreenDevices()(0)
This does not compile, because your second invocation means the same as
ge.getScreenDevices(0)
which does not do what you want because getScreenDevices
is a Java null-ary method which can be invoked either with parentheses or not, and if you specify one set of parentheses, Scala assumes that you want to invoke this method with these parameters, which of course won't work since it does not accept any arguments.
Upvotes: 3