0

I am using json4s library in my scala program.

my build.sbt looks like

libraryDependencies ++= Seq(
  "org.json4s" % "json4s-native_2.11" % "3.3.0"
)

in my code, i have a function

import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.JValue
class Foo {
    def parse(content: String) = {
      val json  = parse(content)
    }
}

but the IDE complains "Recursive method parse needs result type"

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373

1 Answers1

1

The scala compiler usually infers the return type of methods based on their implementations, but it has trouble inferring the type of recursive methods.

The message recursive method parse needs result type is due to this shortcoming. Your def parse(content: String) recurses by calling parse(content). This makes the method recursive (infinitely so, but I'm assuming you were planning on changing it later). In order for it to compile, you'll need to explicitly state the return type, e.g. def parse(content: String): Unit.

I'm going to take a further guess and say that there is a parse method being imported from either json4s or JsonMethods. This is being shadowed by your own parse method due to it having the same method signature. If you actually want to call JsonMethods.parse, then you'll need to actually say JsonMethods.parse to clarify the ambiguity.

Dylan
  • 13,645
  • 3
  • 40
  • 67