Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning "object" in scala

Tags:

object

scala

I'm kinda new to scala. I got into trouble while trying to return object type.

Here is the code.

It shows "error: not found: type A"

object A{}

object B {
  def getInstance() : A = {
    return A
  }
}

If I do similar kind of thing with class instance, it wont show any problem.

class A{}

object B {
  def getInstance() : A = {
    return new A
  }
}

As far as I know object type is a singleton instance of class. What am I missing here?

like image 433
Yoda Avatar asked Mar 22 '23 13:03

Yoda


1 Answers

Compiler complains that can not find type A because in your case A is a name of an object not a type, use A.type to refer to type, like this:

object A

object B {
  def getInstance: A.type = A
}
like image 174
grotrianster Avatar answered Mar 24 '23 03:03

grotrianster