why doesn't scala recognize com.vaadin.ui.MenuBar.MenuItem
문제
I'm trying to create a Vaadin window with a MenuBar using Scala. I'm getting a compiler error that indicates that the com.vaadin.ui.MenuBar.MenuItem import can't be found. I've looked at the Vaadin library (6.4.8), and it appears that the class is there:
com/vaadin/ui/MenuBar$Command.class
com/vaadin/ui/MenuBar$MenuItem.class
com/vaadin/ui/MenuBar.class
Here is the class structure from the MenuBar source:
@SuppressWarnings("serial")
@ClientWidget(value = VMenuBar.class, loadStyle = LoadStyle.LAZY)
public class MenuBar extends AbstractComponent {
...
public interface Command extends Serializable { ... }
public class MenuItem implements Serializable { ... }
}
For demo purposes, here's a sample Scala class:
import com.vaadin.Application
import com.vaadin.ui.Button
import com.vaadin.ui.Window
import com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.Command
import com.vaadin.ui.MenuBar.MenuItem
class MyVaadinApplication extends Application
{
private var window : Window = null
override def init() =
{
window = new Window("My Scala Vaadin Application")
setMainWindow(window)
window.addComponent(new Button("Click Me"))
}
}
And here's the resulting error when I try to compile it:
/Users/jstanford/Development/NetBeansProjects/TraderDashboard/src/main/scala:-1: info: compiling
Compiling 2 source files to /Users/jstanford/Development/NetBeansProjects/TraderDashboard/target/classes at 1291973683915
[ERROR]MyVaadinApplication.scala:7: error: MenuItem is not a member of com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.MenuItem
^
Can anyone spot the problem here?
Thanks, John
해결책
Inner classes cannot be imported as usual it is appropriate for Java. Just specify type projections as it's done for Button#ClickEvent type:
import com.vaadin._
import ui._
import Button._
class MyScalaVaadinApplication extends Application {
val window = new Window("My Vaadin Application")
def init {
window.addComponent(new Button("Click Me", new ClickListener {
def buttonClick(clickEvent: Button#ClickEvent): Unit = window.showNotification("Just a message")
}))
setMainWindow(window)
}
}
다른 팁
A very simple example showing what's going on.
Java:
package a;
public class Outer { public class Inner {} }
Scala:
import a.Outer
import a.Outer.Inner // causes *Inner is not a member of a.Outer*
Notice that Inner, like MenuBar.MenuItem, is a non-static inner class and as such is associated with one instance of Outer. But Scala allows us to import Inner using a instance of Outer:
import a.Outer
import a.Outer.Inner
object dummy {
val o = new Outer
val o2 = new Outer
import o.Inner
val i = new Inner
val i2 = new o2.Inner
}
If we could just import a.Outer.Inner
how would the compiler know to which instance of Outer is Inner supposed to belong to?
Hope the issue has been cleared.