0

The code snippet:

object WTF extends App {
    test
    val mymap = Map("Alice" -> 1, "Bob" -> 2, "Charlie" -> 3, "Dave" -> 4)
    println("outter mymap="+mymap)
    def test { println("inner mymap="+mymap) }
}

produces the following output:

inner mymap=null
outter mymap=Map(Alice -> 1, Bob -> 2, Charlie -> 3, Dave -> 4)
  1. Why is inner printed before outter?

  2. Why is test printing a null mymap?

  3. Is DelayedInit broken on 2.9.2 and is there a way to get inner mymap initialized beside removing "extends App" and writing my own main method?

John Difool
  • 5,572
  • 5
  • 45
  • 80
  • Why would output from code you call at test should go after output at the next step? The code is not broken and explanation of why it's so (mymap is null) in question below. – om-nom-nom Nov 23 '12 at 23:29
  • See also http://stackoverflow.com/questions/13504809/scala-strange-behavior-in-class-object-initialization – om-nom-nom Nov 23 '12 at 23:33

1 Answers1

-1

Problem solved:

object WTF extends App {
    val mymap = Map("Alice" -> 1, "Bob" -> 2, "Charlie" -> 3, "Dave" -> 4)
    println("outter mymap="+mymap)
    test
    def test { println("inner mymap="+mymap) }
}
John Difool
  • 5,572
  • 5
  • 45
  • 80
  • 3
    none of your errors have anything to do with App and/or DelayedInit, care to change the title of the question? – Roland Kuhn Nov 24 '12 at 08:26