Carcigenicate
Carcigenicate

Reputation: 45750

Output is appearing in the wrong order using Scala in Eclipse

I started on Scala this morning, and wrote up the following:

package tutorial
object Main {

    class Tutorial(message: String = "") {
        var a = message

        def speak() =
            println(a)
    }

    def main(args:Array[String]) =
        println("Hello world")

        val t = new Tutorial("BARK!")
        t.speak()

        println("World hello")
}

I expected the output to be:

Hello world
BARK!
World hello

but for some reason, it's outputting:

BARK!
World hello
Hello world

Which makes 0 sense to me. I looked up "Scala buffering problem", but it didn't list anything relevant, so that doesn't seem to be the issue. This is being output to Scala Eclipse's Console. I tried using REPL, but it gives me an error on the first line (package tutorial), even though it compiles fine in Eclipse.

Can someone shed some light on what's going on?

Upvotes: 1

Views: 165

Answers (1)

Guillermo Merino
Guillermo Merino

Reputation: 3257

As Travis Brown suggested, you're missing the {} in your main function.

So what you code should look like is:

package tutorial
object Main {

    class Tutorial(message: String = "") {
        var a = message

        def speak() =
            println(a)
    }

    def main(args:Array[String]) = {
        println("Hello world")

        val t = new Tutorial("BARK!")
        t.speak()

        println("World hello")
    }

}

Upvotes: 1

Related Questions