Skip to content

error on obvious forward ref #221

Closed
Closed
@adriaanm

Description

@adriaanm

From scala-language

On Tuesday, March 22, 2011 at 5:14:16 PM UTC+2, Eugen Labun wrote:
Hi all,
eventually (or even surely) this is an old question but I haven't found an explanation for this
simple situation (nor in Language Specification, nor in Scala-Book).

(More complicated cases are described in this excellent faq from Paul:
https://github.com/paulp/scala-faq/wiki/Initialization-Order)

Constructions like

  def m {
    val x = y // compile error "forward reference extends over definition of value x"
    val y = 5
  }

cause a compile error if used in methods,
but are accepted in constructors/initializers:

  class/object T {
    val x = y // compiles OK, x = 0
    val y = 5
  }

The 'x' in the code above gets initialized to 0 (!), not to 5.

Both -- acceptance by the compiler and initialization to 0 -- are counterintuitive to me.
Would a compile error for the second case ("illegal forward reference") not be a more preferable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions