Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complitation error of "recursive value x needs type" may occur if named parameter used #5434

Closed
scabug opened this issue Feb 5, 2012 · 2 comments

Comments

@scabug
Copy link

scabug commented Feb 5, 2012

See: http://stackoverflow.com/questions/9148339/why-this-scala-code-reports-compilation-error-recursive-value-x-needs-type

Details:

The scala code is simple:

case class X(id: Option[String] = None, username: Option[String])

object X {
  def create(x: X): X = {
    x.copy(id = Some("111"))
  }
}

class Test {
   def test() {
    val x = X.create(X(
      username = Some("Hello, world!")))

    val id = x.id.get  // !!! reports: recursive value x needs type
  }
}

Please note the line:

val id = x.id.get

Why it reports recursive value x needs type?

If I change the variable name, as:

    val dd = x.id.get

It will be OK.

@scabug
Copy link
Author

scabug commented Feb 5, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5434?orig=1
Reporter: Freewind (freewind)
Affected Versions: 2.9.1
Duplicates #5091

@scabug scabug closed this as completed Feb 5, 2012
@scabug
Copy link
Author

scabug commented Jan 30, 2015

@lrytz said:
minimized:

class Test {
  def foo(id: Int = 1, p: Int)

  def test() {
    val x = foo(p = 2)
    val id = x + 1
  }
}

gives

A.scala:7: error: recursive value x needs type
    val id = x + 1  // !!! reports: recursive value x needs type
             ^
A.scala:5: warning: type-checking the invocation of method foo checks if the named argument expression 'id = ...' is a valid assignment
in the current scope. The resulting type inference error (see above) can be fixed by providing an explicit type in the local definition for id.
    val x = foo(p = 2)
            ^
one warning found
one error found

Unfortunately, the warning message doesn't say that the "named argument expression 'id = ...'" was generated by the compiler and is not visible in the source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant