Search code examples
scalacompiler-errorsnamed-parameters

Scala named argument: recursive call


Very strange situation.

I have the following code snippet:

case class SomeResponse(
    ok: Boolean,
    result: Seq[Data]
)

class TestContainer {

    def testMethod() = {

        val response = SomeResponse(
                  // vvv - issue is here
          ok = true, Seq(getStubData)
                  // ^^^
        )

        val result = Json.toJson(response)
    }

    def getStubData = Data(10, "James")
}

When I am not specifying parameter name, everything compiles and works as expected.

But when I explicitly set parameter name

val response = SomeResponse(
           // vvv - issue is here
ok = true, result = Seq(getStubData)
           // ^^^

compiler complies with the following message:

Error:(63, 30) recursive value response needs type
    val result = Json.toJson(response)

Is there any specific compiler behavior for named arguments?


Solution

  • This is the result of the interaction between a semi-solved issue in scala and another one in sbt:

    Judging by the comments there and in linked issues, during type inference the compiler treats all x = y statements in the same way, leading to a cycle when trying to infer result above.

    Workarounds:

    • provide the explicit type of result or even response above; anything that will break the type inference cycle will do
    • rename val result