开发者

"Stable identifer required" error during companion object import

开发者 https://www.devze.com 2023-03-26 04:42 出处:网络
I\'m really new to Scala, and I\'ve come across an error I am unable to solve by myself or through internet searches.

I'm really new to Scala, and I've come across an error I am unable to solve by myself or through internet searches.

I have a Scala class called "GUI" that represents a JFrame along with a companion class. When I try to import the companion class using import GUI._ I get the error "stable identifier required, but GUI.this.GUI() found".

I made an empty class and companion object and the import worked fine, so I assume that the error is related to something specific to my code. Below is the code in question:

object GUI {

   def test:Integer = 1
}

class GUI extends JFrame{

import GUI._
val ICON_LOCATION:File = new File("Images/iMovies.ico");
val ICON:BufferedImage = Ootil.createImage("iMovies.png");
val TITLE:String = "iVideos";
val LICENSE_NAME:String = "OpenBSD";

def GUI(){
    setLayout(new BorderLayout());

    createGUI();

    pack();
    setSize(100,100);
    setLocationRelativeTo(null);
    setVisible(true);
}

def versionMajor: Integer = 1
def versionMinor: Integer = 0
def versionRevision: Integer = 0
def versionPreReleaseID: String = "alpha"
def versionBuildNumber: String = "1b"

private def createGUI():Unit = {
        val panel = new JPanel();
        panel.setLayout(new BorderLayout());


        add(panel, BorderLayout.CENTER);
}

def getIcon():BufferedImage = ICON

de开发者_开发技巧f getProgramTitle():String = TITLE

def getConfigOptions():LookAndFeelConfigurationOptions = GUIConfigOptions.CONFIG_OPTIONS;
}


To add to Kipton's answer, there's nothing wrong with doing:

class GUI{
  def GUI() {
    println("something")
  }
}

But the result won't be a constructor -- it will be an ordinary method.

val a = new GUI() won't print anything, but calling a.GUI() will.

This is why you didn't get an error message about defining your constructor incorrectly.


When you run the command import GUI._, Scala needs GUI to always evaluate to the same object. This is only the case when GUI is an object, a package, or a val.

In your code, import GUI._ referred to the method GUI that you defined, because the GUI method is defined in a closer scope than object GUI (the fact that the compiler hasn't encountered the definition of def GUI yet doesn't make a difference).

Since import GUI._ referred to the method GUI, which is not a val, object, or package, you got the error message about GUI not being a stable identifier.


Welcome to the Scala community.

Scala constructors work differently than they do in Java. To fix the error, you should put the body of your previously defined GUI() method directly into the class definition, like so,

class GUI extends JFrame{
  import GUI._
  val ICON_LOCATION:File = new File("Images/iMovies.ico");
  val ICON:BufferedImage = Ootil.createImage("iMovies.png");
  val TITLE:String = "iVideos";
  val LICENSE_NAME:String = "OpenBSD";

  // ** stuff that used to be in method GUI() goes below **
  setLayout(new BorderLayout());
  createGUI();
  pack();
  setSize(100,100);
  setLocationRelativeTo(null);
  setVisible(true);
  ...
}

It takes a little getting used to, but I think you'll find that Scala's way is a lot nicer.

If you want to define an auxiliary constructor, declare a method def this(...) { ... } whose first expression is a call to the primary constructor (in this case just this(), since it doesn't take parameters).

If you want to add parameters to your primary constructor, you would define the class as

class GUI( << some parameters >> ) extends JFrame { ... }

and then you can use the parameters anywhere in this class body.

0

精彩评论

暂无评论...
验证码 换一张
取 消