Gogolg
Gogolg

Reputation: 13

Compiler incorrectly interpretes as recursive call

Compiler complain that method defined in class A is a recursive one and needs result.type but it's not recursive as I'm calling different method, it's a compiler bug ?

def my(s:String) {

}

class A { def my = my("sss") }

Upvotes: 0

Views: 87

Answers (2)

drexin
drexin

Reputation: 24413

As you can't have methods outside of classes/traits/objects, I guess that A is an inner class. If so, you can do it as follows:

class Foo { outer =>
  def my(s:String) {

  }

  class A { def my = outer.my("sss") }

}

The notation outer => is called a self-type annotation and is used in this form simply to create an alias for the this of class Foo that is not shadowed by the this of the nested type.

edit:

If the outer entity is an object, you can just reference it by its name.

edit2:

If you have overloaded methods it is a different error. If you call an overloaded method from within one of the alternatives, it needs a result type.

def my(s: String) = s
def my: String = my("foo")

// Exiting paste mode, now interpreting.

my: (s: String)String <and> => String
my: (s: String)String <and> => String

scala> my
res0: String = foo

Upvotes: 6

Michael Ford
Michael Ford

Reputation: 861

I think you need to make your def a different name, for example:

class A { def my2 = my("sss") }

Upvotes: 0

Related Questions